Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection refused with postgresql using psycopg2

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.

like image 361
M1nt_zwy Avatar asked Oct 08 '16 06:10

M1nt_zwy


People also ask

Can't connect to server connection timed out psycopg2?

If Django/psycopg2 have the correct domain name or IP address for the PostgreSQL server, and the correct port, the most common explanation for "Connection timed out" is that a firewall (typically on the PostgreSQL server) is filtering that traffic.

What is PostgreSQL psycopg2?

From PostgreSQL wiki. Psycopg2 is a mature driver for interacting with PostgreSQL from the Python scripting language. It is written in C and provides a means to perform the full range of SQL operations against PostgreSQL databases.

Does psycopg2 work with python3?

The current psycopg2 implementation supports: Python 2 versions from 2.6 to 2.7. Python 3 versions from 3.2 to 3.6. PostgreSQL server versions from 7.4 to 9.6.


2 Answers

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.

like image 181
larsks Avatar answered Oct 14 '22 20:10

larsks


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
like image 39
Rt.Tong Avatar answered Oct 14 '22 18:10

Rt.Tong