Skip to content
Download all

A fresh server only listens on localhost. To accept a connection from another machine, three things must all allow it. If any one says no, the connection fails.

yes

yes

yes

no

no

no

Client machine

1. firewalld
port 5432 open?

2. listen_addresses
listening on the network?

3. pg_hba.conf
a matching host line?

Connected

No route to host

Connection refused

no pg_hba.conf entry

Each "no" has its own error message. Learn the three messages and you know which gate to fix.

Gate 1: firewalld. firewalld is the host firewall on CentOS: it decides which network ports other machines may reach on this server. On suta-linux-lab-1, as student:

suta-linux-lab-1 - as student
# Is the host firewall running?
sudo firewall-cmd --state

Expected output:

running

If it prints not running, this server has no host firewall turned on. Skip to Gate 2.

suta-linux-lab-1 - as student
# Open the PostgreSQL port permanently
sudo firewall-cmd --permanent --add-service=postgresql
  • --permanent saves the rule so it survives a reboot.
  • --add-service=postgresql opens TCP port 5432, the port the postgresql service name stands for.
suta-linux-lab-1 - as student
# Load the permanent rules now
sudo firewall-cmd --reload
suta-linux-lab-1 - as student
# Check that postgresql is in the allowed list
sudo firewall-cmd --list-services

Expected output (yours may list other services too):

cockpit dhcpv6-client postgresql ssh

Gate 2: listen_addresses. Still on suta-linux-lab-1, as student:

suta-linux-lab-1 - as student
# Switch to the postgres user (type exit to come back)
sudo su - postgres

Now as postgres on suta-linux-lab-1:

suta-linux-lab-1 - as postgres
# Keep a copy of the original before you edit
cp $PGDATA/postgresql.conf $PGDATA/postgresql.conf.orig
suta-linux-lab-1 - as postgres
# Open the main configuration file in vi
vi $PGDATA/postgresql.conf

In vi:

  1. Type /listen_addresses and press Enter. The cursor jumps to the line #listen_addresses = 'localhost'.
  2. Press 0 (zero) to move to the start of the line.
  3. Press C (capital C). This deletes the whole line from the cursor and puts you in insert mode.
  4. Type listen_addresses = '*'
  5. Press Esc.
  6. Type :wq and press Enter to save and quit.

'*' means "listen on every network address of this server". The old line started with #, which made it a comment, so the server was using the default, localhost.

suta-linux-lab-1 - as postgres
# Check the new line (-n shows the line number)
grep -n '^listen_addresses' $PGDATA/postgresql.conf

Expected output (your line number may differ):

60:listen_addresses = '*'

Gate 3: pg_hba.conf. Still as postgres on suta-linux-lab-1:

suta-linux-lab-1 - as postgres
# Keep a copy of the original before you edit
cp $PGDATA/pg_hba.conf $PGDATA/pg_hba.conf.orig
suta-linux-lab-1 - as postgres
# Open the client access file in vi
vi $PGDATA/pg_hba.conf

In vi:

  1. Press G (capital G) to jump to the last line.
  2. Press o (lowercase o) to open a new empty line below it, in insert mode.
  3. Type this line, with your client machine's IP address in place of CLIENT_IP: host appdb appuser CLIENT_IP/32 scram-sha-256
  4. Press Esc.
  5. Type :wq and press Enter to save and quit.

The line means: allow role appuser, to database appdb, from that one address (/32 means exactly one IP), with a password.

suta-linux-lab-1 - as postgres
# Return to the student user
exit

Apply the changes. listen_addresses needs a restart. On suta-linux-lab-1, as student:

suta-linux-lab-1 - as student
# Restart PostgreSQL so it re-reads listen_addresses and pg_hba.conf
sudo systemctl restart postgresql-18
suta-linux-lab-1 - as student
# Confirm PostgreSQL now listens on all addresses, port 5432
sudo ss -tlnp | grep 5432
  • ss lists network sockets. -t TCP, -l listening, -n numbers instead of names, -p show the program.
  • | grep 5432 keeps only the lines that mention port 5432.

Expected output:

LISTEN 0 200 0.0.0.0:5432 0.0.0.0:* users:(("postgres",pid=1498,fd=8))
LISTEN 0 200 [::]:5432 [::]:* users:(("postgres",pid=1498,fd=9))

0.0.0.0:5432 means "every address". Before the change it said 127.0.0.1:5432.

Test from the client machine. On the client machine, as student:

client machine - as student
# Connect over the network as appuser to appdb (enter the password when asked)
psql -h SERVER_IP -U appuser -d appdb
  • -h is the server's address (host). Replace SERVER_IP with the private IP address of suta-linux-lab-1.
  • -U is the role to connect as.
  • -d is the database.

Inside psql, as appuser (this line is SQL, copy it exactly):

psql - as appuser
-- Show who you are connected as, and the server address you reached
SELECT current_user, inet_server_addr();

Expected output (your address will differ):

current_user | inet_server_addr
--------------+------------------
appuser | 192.168.215.2
(1 row)

Was this page helpful?

Thanks. Glad it helped.

Thanks for telling us. Email the instructor what was missing, or ask in class.