繁体   English   中英

无法将PostgreSQL与Django连接

[英]Not able to connect postgresql with django

我已经在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中的通用端口。

然后执行syncdb或迁移。

由于您的表格是在没有Django工具的情况下创建的。 您将需要写下一个Model类,并设置一些有关表的“自定义”名称格式的Metal信息: https : //docs.djangoproject.com/en/1.7/ref/models/options/#table-names

如果可以,我建议您删除该表并使用Django ORM建立数据库。 它比用SQL编写所有内容更快,更简单: https//docs.djangoproject.com/en/1.7/intro/tutorial01/#creating-models

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM