I am unable to connect to postgres database through remote host using psycopg2 and getting the error like
Error:
File "/usr/lib64/python2.7/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "192.x.x.x" and accepting
TCP/IP connections on port 5432?
Note: I have made following changes
1.updated the pg_hba config file
host all all 0.0.0.1/32 trust
2.updated the postgresql file
listen_addresses = '*'
3.changed the firewall rule to allow connection from port 5432
What else I have to do to make it work?
For others that might encounter the same issue, here are some things you can try.
Make sure the service is listening on the needed interfaces
sudo ss -lntp | grep 5432
If you see something like 127.0.0.1:5432 or ::1:5432 this means localhost.
Tweak postgresql.conf:
listen_addresses='192.168.1.2, 127.0.0.1'
You can also use '*' which means any interface but depending on your network configuration it might be dangerous.
Make sure the listen_addresses line isn't commented (as was the case above) and restart the service after making changes.
Check your firewall configuration
After making sure the service is listening on the correct interface make sure your firewall(s), if any, permit the client to connect to the service (this can mean your local firewall, a network device sitting between the client and the service).
Check pg_hba.conf
This file controls PostgreSQL's host-based authentication mechanism:
# TYPE DATABASE USER ADDRESS METHOD
host all all 192.168.1.3/32 md5
The line above allows all users to connect from 192.168.1.3 if they provide the required password; if you want to allow a whole subnet you can use something like 192.168.1.0/24.
If you suspect an issue with psycopg2 or Python you can test the connectivity using PostgreSQL's client: psql:
psql -U postgres -h 192.168.1.2 db_name
Related
For now I am just trying to make the heroku pg:psql command work but my final purpose is to copy a database that I have on my computer (localhost) to the heroku postgresql database with the pg:push command.
For now when I simply try to access the database that I created on heroku, the heroku pg:psql command returns:
psql: could not connect to server: No route to host
Is the server running on host "ec*-**-***-***-**.eu-west-1.compute.amazonaws.com" (**.***.***.**) and accepting
TCP/IP connections on port 5432?
postgresql.conf: (the lines are not commented)
listen_address ='*'
port = 5432
ssl = true
and host all all **.***.***.** trust in pg_hba.conf
I also tried to add rules to iptables in order to give access to the database from the host IP address provided by heroku.
I am on a Debian computer, how can I solve this?
psql: could not connect to server: No route to host
It means your PostgreSQL server is not starting up or is starting up on a different port.
Solutions you may try:
Check PostgreSQL service by command ps -ef | grep Postgres.
Check the port which PostgreSQL is listening to by command netstat -tupln | grep Postgres.
Make sure your server enables UDP port because PostgreSQL needs UDP port loopback for stats collector service.
Check the startup logs or database logs at pg_log about the problem.
I missed a bit with chmod and permissions.
And now got a lot of problems with access to folders.
If I start server I see in browser:
could not connect to server: Connection refused Is the server running
on host "localhost" (::1) and accepting TCP/IP connections on port
5432?
could not connect to server: Connection refused Is the server
running on host "localhost" (127.0.0.1) and accepting TCP/IP
connections on port 5432?
command
service --status-all
tells that postgres works
[ + ] postgresq works
Next I changed next files
sudo nano /etc/postgresql/9.5/main/postgresql.conf
#listen_addresses = 'localhost'
to
listen_addresses = 'localhost, server_ip, *'
sudo nano /etc/postgresql/9.5/main/pg_hba.conf
add
host all all server_ip/24 trust
then command
psql -U postgres -h server_ip
and error
psql: could not connect to server: Connection refused
Is the server running on host "95.213.200.26" and accepting
TCP/IP connections on port 5432?
The following chain of actions leads to a slightly different error
command
which psql
answer
/usr/bin/psql
sudo su - postgres
No directory, logging in with HOME=/
psql
psql: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Wich file permissions do I have to change here?
Check that unix_socket_directories on the server is set to the same directory (/var/run/postgresql) as the default socket directory that was configured when the client was compiled (this requires a server restart).
Alternatively, you can use the --host option of psql or set the PGHOST environment variable to tell the client to look for the socket file in /tmp, where the server creates it.
It looks like client and server are from different installations, otherwise the configured default should be the same for both. Or you have a bad PGHOST environment variable set by accident.
Check listen addres syntax. Use:
listen_addresses = '*'
For the server to listen on both localhost and the server ip.
This question is really hard. I made a great mistake with permissions 777 for / folder and whole system broke. So it is really easier delete server and start new project.
I'm having issues connecting to my PostgreSQL database from an online source.
PostgreSQL seems to be setup up fine it is running on it's default port 5432 and the postgresql.conf has the following line
listen_addresses = '*'
And the pg_hba.conf has the following
host dbname usname all md5
I can connect to the PostgreSQL database from a different machine using the following credentials in pgadmin
Name local
Host 192.xx.xx.xx
Port 5432
Maintenance DB dbname
Username usname
Password psword
Using these I can connect perfectly it seems to work fine.
Then I forwarded the port 192.xx.xx.xx:5432 on the ADSL router and checked it using the public IP address 197.xx.xx.xx (visible to the internet IP)
http://www.canyouseeme.org/
Where I can see the port was forwarded ok.
However when I try to connect using
Name online
Host 197.xx.xx.xx
Port 5432
Maintenance DB dbname
Username usname
Password psword
I get the server doesn't listen error message
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "197.xx.xx.xx" and accepting TCP/IP connections on port 5432?
Why is this? Do I need to add an extra step to making it accept connections through port forwarding?
I want to connect to postgresql from another server but occur below error. i search and find some result like below but after do these again error occured
in postgresql.conf change listen_addresses from localhost to *
in pg_hba.conf change 127.0.0.1/32 to 0.0.0.0/0
reboot server
Error:
could not connect to server: Connection refused.Is the server running
on host "Ip Number"
and accepting.TCP/IP connections on port 5432?
I suggest that you should do as the following steps.
Check your PostgreSQL SERVER is running.
Check the listen_addresses parameter ( postgresql.conf )
Check the port number of your postgresql ( The port parameter of
postgresql.conf )
Check your the firewall policy of your database server , set
that allow the port of your postgresql.
If you want to connect your db from another server, may be you
shoud set the pg_hba.conf as following :
# TYPE DATABASE USER CIDR-ADDRESS METHOD
local all all trust
host all all 0.0.0.0/0 md5
Try to check if the network traffic reaches the server. You can sniff the network traffic to see what happen. Meassure on client and server and check the communication.
sudo tcpdump -A -n -s 0 -w test_on_client.log port 5432
After you try your connect stop tcpdump with CTRL-C and inspect the log file with Wireshark.
This procedure will find every network connection problems.
I just recently install PostgreSQL on our server via SSH. The installation went successful, until the time I tried to connect to it using pgAdmin on my Windows machine.I received this kind of error:
could not connect to server: Connection refused (0x0000274D/10061) Is
the server running on host "xxx.xxx.xxx.xxx" and accepting TCP/IP
connections on port 5432?
xxx.xxx.xxx.xxx = my server's public IP.
The docs suggest this can be fixed by setting the value of listen_addresses = '*' in the /etc/postgresql/9.1/main/postgresql.conf. I did that but still it won't let me.
additional error came up
FATAL: no pg_hba.conf entry for host "xxx.xx.xxx.xxx", user
"postgres", database "postgres", SSL on FATAL: no pg_hba.conf entry
for host "xxx.xx.xxx.xxx", user "postgres", database "postgres", SSL
off
xxx.xx.xxx.xxx = my IP address.
What seems to be I'm missing?
Things that could block a postgres connection:
misconfigured listen_address in postgresql.conf
selinux (?)
iptables
pg_hba.conf (although this should cause a different error, not server doesn't listen)
Can you connect to the server locally, if you ssh in and run psql?
On our internal dev servers, I just turn off selinux and iptables. This is a bad idea from a security standpoint, but it might serve as a temporary step to help you narrow down where the problem is.
You might need to change more than one configuration file. In your case, you probably need to edit pg_hba.conf, too. Search that file for "non-local connections".
I like to keep configuration files under version control. It's easier to recover from mistakes that way.
You probably need to restart the PostgreSQL server after making those changes.
After changing listen_addresses settings on the server, make sure to restart the PostgreSQL server (send SIGHUP to the postmaster process, with kill -HUP, etc).
Make sure that postgresql.conf port is set to 5432
Make sure that if a firewall is running on the server, that port 5432 is open for connections coming from the window's (client) machine you are using
Check pg_hba.conf to make sure that the subnet of your client machine is given access
Try using psql locally