Initial problem
I am trying to remotely connect myself to my pgsql database, but when I do
psql -h 192.168.0.121 -p 5432 my_pgsql_admin_username -d my_database
(192.168.0.121 being the address of the machine hosting the pgsql database), I got the tipical error :
psql: could not connect do server: Connection refused
Is the server running on host "192.168.0.121" and accepting
TCP/IP connections on port 5432 ?
My configuration
My /etc/postgresql/9.5/main/postgresql.conf already contains a listen_adresses = '*'.
My /etc/postgresql/9.5/main/pg_hba.conf contains the following rules :
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all 192.168.0.0/24 md5
host all all ::1/128 md5
A sudo iptables -L gives me :
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere 192.168.0.121 tcp spts:1024:65535 dpt:postgresql state NEW,ESTABLISHED
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 192.168.0.121 anywhere tcp spt:postgresql dpts:1024:65535 state ESTABLISHED
I already restarted my postgresql (sudo service postgresql restart), and my iptables (/etc/init.d/networking restart). I also tried to replace md5 by trust in my pg_hba.conf, without seeing any changes.
Weird behavior
In my postgresql.conf if I don't set listen_adresses, I can't remotely connect, but at least I can locally connect (with a simple psql), but if I set the listen_adresses varaible to anything (may it be '*' or 'localhost') then I'll still not be able to remotely connect, but I even won't be able to connect locally.
Question
What is going wrong here ? Is it a strange bug or did I just missed something in the configuration ?
System specs :
Ubuntu 16.04.1 on a virtualBox
PostGreSQL 9.5
I just misspelled "listen_addresses" with only one 'd'. 4 hours of work wasted for ... being dumb and french I guess ...
Related
I have access remote postgesql psql -h xxx.xxx.xxx.xxx -p 1486 postgres
It's show this error
psql: could not connect to server: Connection refused
Is the server running on host "xxx.xxx.xxx.xxx" and accepting
TCP/IP connections on port 1486?
postgresql.conf file
listen_addresses = '*'
pg_hba.conf file
host all all 0.0.0.0/0 md5
Also apply open 1486 port
iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d X.X.X.X --dport 1486 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp -s X.X.X.X --sport 1486 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
It's return same error.
It's hard to give a canonical answer but here are a few things to try (some of them may not apply / be possible):
Ping the IP address in question - is the server up at all / reachable? Are there other services on the box that can be reached?
Connect from a local connection on the server itself, assuming you have console or ssh access
If you have local access and network based access doesn't work, is unix-domain socket access allowed and if so, does that work?
Check the port config in postgresql.conf - is it really where you think it is?
Has the config file been edited since the last server restart? The parameters you listed all require server restarts to take effect.
Is it actually using the config file you think it is? Running "SHOW config_file;" as superuser will help if you can make a local connection
What happens is you do telnet 1486? Do you get a network connection or similar error?
Get a pcap and check what's happening at the network level (wireshark or tcpdump will help)
I've been trying to make Postgres available over the network but so far have been unable to do so.
According to netstat, postgres is listening on the right port:
#netstat -anltp | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2385/postgres
But when I try to nmap from another device, the port appears to be closed.
#nmap -p 5432 marvin
Starting Nmap 6.40 ( http://nmap.org ) at 2014-10-31 10:31 CET
Nmap scan report for marvin (*.*.*.*)
Host is up (0.00048s latency).
rDNS record for *.*.*.*: marvin.*.*
PORT STATE SERVICE
5432/tcp closed postgresql
My guess is, it has to do something with the fact that I'm using a hostname instead of an IP, but since the IP changes here every so often, I'd rather use the hostname.
I've already set listen_addresses = '*', which was the solution to another similar problem I've found here, but to no avail. I've also experimented with different settings in the pg_hba.conf but the port remained closed.
host all all .jarvis trust
host all all jarvis trust
host all all 0.0.0.0/24 trust
I've also used several examples I've found for my iptables, but no luck there either.
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:postgresqlflags: FIN,SYN,RST,ACK/SYN
ACCEPT tcp -- anywhere Marvin tcp spts:1024:65535 dpt:postgresql state NEW,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:postgresql
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- Marvin anywhere tcp spt:postgresql dpts:1024:65535 state ESTABLISHED
And lastly, I've tried to connect via telnet, no luck there either
telnet marvin 5432
Trying *.*.*.*...
telnet: Unable to connect to remote host: Connection refusedo connect to the port
Yet, there are no issues on 22.
telnet marvin 22
Trying *.*.*.*...
Connected to marvin.*.*.
Escape character is '^]'.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1
Can anyone tell me if there's a setting somewhere I misinterpreted or of its an issue with using hostnames?
Look at your netstat output again. You are only listening on localhost (127.0.0.1). Check your config file and restart PostgreSQL.
I am trying to enable Postgresql 9.3 to accept remote connections on CentOS6. I opened the port in iptables, set the port to 5432 in the pgconf file, set listen_addresses to '*' (accept all connections), and allow addresses in the pg_hba with host all all 0.0.0.0/0 trust. The postmaster is running on 5432. However, I still receive the following error. How do I get a valid connection?
The error:
could not connect to server: Connection refused
Is the server running on host "50.63.141.236" and accepting
TCP/IP connections on port 5432?
Relevant part of pg_hba.conf file:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
#opens postgres to the internet
host all all 0.0.0.0/0 trust
Relevant output from iptables -L:
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:postgres
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:postgres
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:postgres
Finally, proof that postmaster is running.
EN 1433/sshd
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LIST
You haven't restarted PostgreSQL after changing listen_addresses. It's still listening on 127.0.0.1, i.e. loopback only. Or you edited the config file for a different PostgreSQL install.
If you connect to host 127.0.0.1 port 5432 it'll work. Or make sure you edited the right config and restart (not just reload) PostgreSQL.
Hello all :) I'm a having a little trouble connecting this.
On Windows 7 about my Debian 6 on VitualBox configured with Host-only Adapter:
>nmap -T4 -A -v 192.168.56.1
[...]
5432/tcp unknown postgresql
On the Debian, PostgreSQl is listening:
>netstat -tulpn
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 2432/postgres
tcp6 0 0 :::5432 :::* LISTEN 2432/postgres
.. and the port is opened
>iptables -nL
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0./0 tcp dpt:5432
.. and Postgres is accepting all the connections in postgresql.conf
listen-addresses = '*'
port = 5432
In Windows I have this error message from pdAdmin:
Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "192.168.56.1" and accepting TCP/IP
connections on port 5432?
If you encounter this message, please check if the server you're trying
to contact is actually running PostgreSQL on the given port.
Test if you have network connectivity from your client to the server
host using ping or equivalent tools. Is your network / VPN / SSH tunnel /
firewall configured correctly?
For security reasons, PostgreSQL does not listen on all available
IP addresses on the server machine initially. In order to access
the server over the network, you need to enable listening on the
address first.
For PostgreSQL servers starting with version 8.0, this is controlled
using the "listen_addresses" parameter in the postgresql.conf file.
Here, you can enter a list of IP addresses the server should listen
on, or simply use '*' to listen on all available IP addresses. For
earlier servers (Version 7.3 or 7.4), you'll need to set the
"tcpip_socket" parameter to 'true'.
You can use the postgresql.conf editor that is built into pgAdmin III
to edit the postgresql.conf configuration file. After changing this
file, you need to restart the server process to make the setting effective.
If you double-checked your configuration but still get this error
message, it's still unlikely that you encounter a fatal PostgreSQL
misbehaviour. You probably have some low level network connectivity
problems (e.g. firewall configuration). Please check this thoroughly
before reporting a bug to the PostgreSQL community.
Best regards
What about your pg_hba.conf file?
Have you configured it to accept connections from hosts in the 192.168.56.0 network?
Try to add this line and restart Postgres:
# VitualBox Host-Only Adapter
host all all 192.168.56.0/24 md5
If it's a testing environment you could even replace 192.168.56.0/24 with 0.0.0.0/0 and forget about it.
So I'm using AWS using EC2 and I'm trying to open up a port for Postgresql. In AWS I already have it open:
TCP
Port (Service) Source Action
0 - 65535 sg-92aadda2 (default) Delete
22 (SSH) 0.0.0.0/0 Delete
80 (HTTP) 0.0.0.0/0 Delete
5432 0.0.0.0/0 Delete
When I do netstat it looks as though the port is listening:
# netstat -an | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
When I do a localhost nmap I get the following:
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000010s latency).
Not shown: 997 closed ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
5432/tcp open postgresql
And here's where the fun begins. When I do an nmap from an alternative host I get the following:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
5432/tcp closed postgresql
I also looked at my iptables to see if I was missing something, but the iptables look empty (which should mean they aren't really doing much)
$ iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
REJECT all -- anywhere 127.0.0.0/8 reject-with icmp-port-unreachable
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:postgresql
ACCEPT icmp -- anywhere anywhere
LOG all -- anywhere anywhere limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
Am I missing something cause I can't seem to figure out how to access the ip. Whenever I try I get the following error:
Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?
How do I make it so that I can open up the port so that external servers have access to it? Thanks in advance =) Lemme know if you need any additional data.
EDIT: As asked below, I tested telnetting, and I was able to telnet into the localhost, but when attempting from the outside I get:
$ telnet xx.xx.xx.xx 5432
Trying xx.xx.xx.xx...
telnet: Unable to connect to remote host: Connection refused
Also, I double checked and I was properly able to telnet into ssh:
$ telnet xx.xx.xx.xx 22
Trying xx.xx.xx.xx...
Connected to xx.xx.xx.xx.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
Edit /etc/postgresql/<version>/main/postgresql.conf and set the listen_addresses to your outgoing interface or all. Restart postgresql: sudo service postgresql restart.
It works for me the last method (thks Julio):
Edit: postgresql.conf
sudo nano /etc/postgresql/9.3/main/postgresql.conf
Enable or add:
listen_addresses = '*'
Restart the database engine:
sudo service postgresql restart
Besides, you can check the file: pg_hba.conf
sudo nano /etc/postgresql/9.3/main/pg_hba.conf
And add your network or host address:
host all all 192.168.1.0/24 md5
If you have edited postgresql.conf and main/pg_hba.conf and still having a problem, please try
sudo ufw allow 5432/tcp
to unblock psql port
In case you are using docker to connect to the host's postgresql you have to use the host's ip which you can obtain by running ip addr show docker0 hope it helps someone.