33

I finally (think) installed successfully PostgreSQL and also de psycopg2 (I use Windows). Btw, is some way to check it's working properly?

Well, the thing now is that I can't start the server, while I type 'python manage.py runserver' it shows this (at the end of the command):

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

I've searched a lot of documentation about it, for example in this topic, but I can't find the way to make it work properly. I've tried several changes in the pg_hba and postgresql files, but with no exit. In this moment, pg_hba looks like:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1            md5
# IPv6 local connections:
host    all             all             127.0.0.1            md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

And postgresql conf looks like:

# - Connection Settings -

listen_addresses = 'localhost'      # what IP address(es) to listen on;
                # comma-separated list of addresses;
                # defaults to 'localhost'; use '*' for all
                # (change requires restart)
port = 8000             # (change requires restart)
max_connections = 100           # (change requires restart)
# Note:  Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = ''   # comma-separated list of directories
                # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
                # (change requires restart)
#bonjour = off              # advertise server via Bonjour
                # (change requires restart)
#bonjour_name = ''          # defaults to the computer name

Btw, my settings.py of the database look like this:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database1',                      
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': 'localhost',
    'PORT': '8000',
    }
}

I haven't created a database BTW, how should I do it? What are the applications of the PostgreSQL prompt?

I would highly appreciate help with this issue I've been days searching and trying but with no exit. Thank you very much.

EDIT 1: I tried changing the settings.py port to 5432, but now the error message is the same, just changing the port:

could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

The config files are right this way? Should I change something? I can't find an answer. I tried with python manage.py runserver and both indicating the 127.0.0.1:8000 and 8001, but no changes in the error message. What's going wrong? Thank you very much.

Somangshu Goswami
  • 1,098
  • 11
  • 27
Jim
  • 541
  • 1
  • 7
  • 15
  • 5
    Django development server runs on port 8000 by default. Why did you choose the same port for your PostgreSQL server? Just leave the default (5432) and try again. The connection should be ok without touching any config files. – Selcuk Feb 17 '16 at 11:18
  • Does it have to be in a specific directory or something? Just tried but can't figure out why the error message persists... thankyou so much. – Jim Feb 17 '16 at 15:05

10 Answers10

25

For me it was for postgres not being started and enabled. So I solved the problem by doing so:

sudo systemctl start postgresql
sudo systemctl enable postgresql
Ghasem
  • 14,455
  • 21
  • 138
  • 171
8

Make port default to 5432

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database1',                      
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': 'localhost',
    'PORT': '5432',
    }
}

run the command:

python manage.py runserver 127.0.0.1:8000
Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
  • Now the message error includes: could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? Should I change something in the notepad files?? Thank you both for the answers. – Jim Feb 17 '16 at 11:52
  • 2
    `python manage.py runserver` try this or `python manage.py runserver 127.0.0.1:8001` – Usman Maqbool Feb 17 '16 at 12:18
  • Hi, thank you for the answers. I run the command and changed the ports but the error message is still there. Connection refused still happening. I've edited the first post explaining what happens when changing the port. Should i make some changes in the config files? I'm trying everything but can't figure out what's wrong... thank you all again – Jim Feb 17 '16 at 13:53
  • 2
    restart your Postgres, Hopefully that fixes the problem... Otherwise your port 5432 might be blocked for some reason. – Usman Maqbool Feb 17 '16 at 16:12
  • how could I restart it? I hardly understand postgresql prompt, there's maybe some convention with django in how directories have to be or how to enable database connection in postgresql. Thank you for replying – Jim Feb 17 '16 at 19:28
  • 1
    Isn't there any solution really? :( so sad, think it's imposible to work with postgresql... – Jim Feb 17 '16 at 22:01
  • 4
    There is possibility that server is not running. `sudo service postgresql start` or `sudo /etc/init.d/postgresql start` – Usman Maqbool Feb 18 '16 at 07:17
6

I just had the same problem and I just had forgotten to start my Postgres. Opening it from my Applications and selecting "Open psql" solved the problem.

scollaco
  • 947
  • 8
  • 13
5

If you are using Postgresql and developing on a mac then you can use the below command to start Postgresql and it should fix your problem.

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

and to stop it

pg_ctl -D /usr/local/var/postgres stop -s -m fast
Addicted
  • 149
  • 1
  • 3
  • 9
5

Try removing host and port:

# settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_pass',
    }
}

The problem for me was specifying HOST and/or PORT here.

zMeadz
  • 51
  • 1
  • 2
1

I faced a similar issue and I solved it by changing the value of "HOST": "localhost" to "HOST": "127.0.0.1" and it worked.

0

Credit to Addicted above. I checked the log and realized at some point I had upgraded my Postgres version.

$ nano server.log

  GNU nano 2.0.6                               File: server.log                                                                     

2020-12-16 02:19:32.273 EST [32414] FATAL:  database files are incompatible with server
2020-12-16 02:19:32.273 EST [32414] DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.

Which can be solved like this.

Ryan Skene
  • 864
  • 1
  • 12
  • 29
0

no database server is hosted and running locally. For mac, you can turn on a server on port 5432 using the postgres desktop application.

0

I Had the same problem but in my case its due to memory shortage. Due to memory shortage the postgresql was not able to create the file .s.PGSQL.5432 inside /var/run/postgresql/. So Look for the memory storage and if there is no memory available ,please free some memory and try restarting postgresql service.

0

I had the same problem because I forgot to run docker.

matpvl
  • 63
  • 6