0

在 vagrant box 上,通过apt-get install python-psycopg2. 测试导入成功。

$ python
Python 2.6.5 (r265:79063, Oct  1 2012, 22:04:36) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> exit()

Django 设置文件:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '/vagrant/v-root/projects/tdg/tdg/mydata.db',                      # Or path to database file if using sqlite3.
        'USER': 'username',                      # Not used with sqlite3.
        'PASSWORD': 'password',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

在新的 vagrant box 上找不到 postgres_psycopg2 的问题。

$ python manage.py shell
Python 2.6.5 (r265:79063, Oct  1 2012, 22:04:36) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.db import connection
>>> cursor = connection.cursor()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 177, in _cursor
    self.connection = Database.connect(**conn_params)
OperationalError: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

>>> 

我跑了一个netstat —listen,我没有看到端口 5432 正在监听。只有 22 个(SSH)和 8000 个(Django 应用程序)在监听。

4

1 回答 1

1

您没有完全安装 postgres - 您只是安装了 psycopg2,它是 postgres 的 python 客户端。您需要像这样安装和运行 postgres:

apt-get install postgresql
/etc/init.d/postgresql start

(您可能还需要登录并为自己创建一个数据库供您的项目与之交谈)

于 2013-01-30T22:31:24.980 回答