DanielTheRocketMan
DanielTheRocketMan

Reputation: 3249

Django and postgres: Not connecting

I have been trying to use Django with postgress, but it seems that I am having a kind of connection problem.

It seems that my problem is related to the settings.py file, where presents these settings for interaction with the database:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '8000',
    }
}

When I remove these lines above from the settings.py,

python manage.py runserver 

runs the server as expected. On the other hand, when these lines are present I receive the message:

conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 8000?

This problem could have been predict since it arose first when I tried to "migrate"

python manage.py migrate

As you can see below, my user has the following attributes:

Role name |                   Attributes                   | Member of | Description 
-----------+------------------------------------------------+-----------+-------------
 myuser    | Superuser, Create DB                           | {}        | 
 postgres  | Superuser, Create role, Create DB, Replication | {}        | 

Finally, it seems that I do not have any problem with the installation of psycopg2, since I was able to create mydb as presented below. In fact, the list of databases of my system is

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 mydb      | myuser   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

Should I include new attributes? Which? Is something else? I am pretty new with Django and postgres and any help is very welcome.

By the way, I am using Ubuntu 14.04, Python 3.4 and Django 1.7.1.

Upvotes: 2

Views: 4965

Answers (1)

rchang
rchang

Reputation: 5236

I would check postgresql.conf to confirm the port that PostgreSQL is using to listen. The default is normally 5432, not 8000, so unless you explicitly configured PostgreSQL to use 8000 I would suspect this is at least part of your problem, and the error message would certainly be explained by that (although it may not be the only possible cause).

Upvotes: 5

Related Questions