22

psycopg2.OperationalError: could not connect to server: Connection refused

Is the server running on host "45.32.1XX.2XX" and accepting TCP/IP connections on port 5432?

Here,I've open my sockets.

tcp        0      0 127.0.0.1:5432          0.0.0.0:*  LISTEN      11516/postgres                
tcp6       0      0 ::1:5432                :::*       LISTEN      11516/postgres

I googled that I should modify this pg_hba.conf,but in my postgresqlroot files, I didn't find this file at all.

Also I've succeed in connecting my another server.

Thanks.

Here,I've modified the pg_hba.conf,updated thishost all all 218.3.A.B trust and reloaded.But it didn't work either.

2
  • Under what operating system is running PostgreSQL? Commented Oct 8, 2016 at 8:17
  • in Ubuntu 14.04 Commented Oct 8, 2016 at 9:02

4 Answers 4

17

Your netstat output shows that postgres is listening on 127.0.0.1, but your error suggests you are trying to connect to 45.32.1XX.2XX. I am pretty sure you have already diagnosed your problem.

You will need to modify the listen_addresses setting your postgresql.conf file (not pg_hba.conf). The postgresql.conf file is found in your postgresql data directory, which is often something like /var/lib/postgresql/data or /var/lib/pgsql/data.

The listen_addresses parameter is documented here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot.Finally i append this listen_addresses = '*' into postgresql.conf.And it works well.
6
1. Modify two configure files
# vi /var/lib/pgsql/data/postgresql.conf
 Replace the line:
listen_addresses = 'localhost'  -> listen_addresses = '*'
# vi /var/lib/pgsql/data/pg_hba.conf
 Add the line at the very end:
host all all 0.0.0.0/0 trust
(If IPv6:
host all all ::/0 trust) 
2. Restart the database service
# service postgresql restart
3. Disable the firewall
# rcSuSEfirewall2 stop
# chkconfig SuSEfirewall2 off
# chkconfig SuSEfirewall2_init off

1 Comment

The accepted answer did not work for me. But after modification of the pg_hba.conf file, it worked. Also, i'm on postgresql 11
1

Do make sure that you've started postgress with systemctl start postgresql

1 Comment

=))) thanks, It is foolish of me to make this mistake
0

Uncommenting listen_address line and introducing '*' worked for me. In the beginning was working without it (gunicorn with Postgres database)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.