python - 无法将 postgresql 与 django 连接

标签 python django database postgresql ubuntu-12.04

我在 postgresql 中创建了一个名为 testdb1 的数据库,我正在尝试将它与我的 django 应用程序连接。但是当我运行 python manage.py syncdb 时,我收到错误 django.db.utils.OperationalError: FATAL: database "testdb1"does not exist。该数据库确实存在,因为它已通过以下内容确认:

archit@archit-Inspiron-5520:~/Documents/DjangoLabs/gswd$ psql -U postgres
Password for user postgres: 
psql (9.1.14)
Type "help" for help.

postgres=# \connect testdb1
You are now connected to database "testdb1" as user "postgres".
testdb1=# \d
          List of relations
 Schema |  Name   | Type  |  Owner   
--------+---------+-------+----------
 public | company | table | postgres
(1 row)

settings.py 我有:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'testdb1',                      
        'USER': 'postgres',
        'PASSWORD': 'admin',
        'HOST': 'localhost',
        'PORT': '',
    }
}

/etc/postgresql/9.1/main/pg_hba.conf 中:

# Database administrative login by Unix domain socket
local   all             postgres                                md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5
local   all         postgres                          md5

编辑:

\du 命令给出:

testdb1=# \du
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 archit    | Superuser, Create role, Create DB, Replication | {}
 postgres  | Superuser, Create role, Create DB, Replication | {}

如有任何帮助,我们将不胜感激。

最佳答案

删除数据库并重新创建数据库。

在 settings.py 中写入 'PORT'='5432' 或 '5433' 因为这是所有 postgresql 中的公共(public)端口。

然后做syncdb或者migrate。

关于python - 无法将 postgresql 与 django 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27742212/

相关文章:

python - 从 python struct.pack (big-endian) 转换为整数列表

python - Django依赖: hmac?

java - 在 SQL 语句中实现动态变量时,是否有比字符串连接更好的方法

Django-rest-auth 使用 cookie 而不是 Authorization header

python - 在 django 模板中渲染 Bokeh 小部件

java - Apache Derby 为我创建的具有有意义名称的索引提供了奇怪的名称

java - SQLite 类未找到异常 - 使用 Tomcat 的 Maven 项目

javascript - 重新加载当前页面时,Django 不会刷新我的请求对象

python - rpy2代码可以并行运行吗?

python - 减少 python 中 sqlite3 execute/fetchone 的时间