简体   繁体   中英

Not able to connect postgresql with django

I've created a database named testdb1 in postgresql and I am trying to connect it with my django app. But when I run python manage.py syncdb I get the error django.db.utils.OperationalError: FATAL: database "testdb1" does not exist . The database does exist as it is confirmed by the following:

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)

In settings.py I have:

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

In /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

EDIT:

\\du command gives:

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

Any help would be appreciated.

Drop database and re create database.

In settings.py write 'PORT'='5432' or '5433' as this is the common port in all postgresql.

Then do syncdb or migrate.

Since your table was created without the tools of Django. You will need to write down an Model class and set some Metal information about the "custom" name format of your table: https://docs.djangoproject.com/en/1.7/ref/models/options/#table-names

If you can, i recommend you to drop this table and use the Django ORM to build up your database. It's more fast and simple than write everything in SQL: https://docs.djangoproject.com/en/1.7/intro/tutorial01/#creating-models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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