7.2 Allow remote connections
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.
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:
# Is the host firewall running?sudo firewall-cmd --stateExpected output:
runningIf it prints not running, this server has no host firewall turned on. Skip to Gate 2.
# Open the PostgreSQL port permanentlysudo firewall-cmd --permanent --add-service=postgresql--permanentsaves the rule so it survives a reboot.--add-service=postgresqlopens TCP port 5432, the port thepostgresqlservice name stands for.
# Load the permanent rules nowsudo firewall-cmd --reload# Check that postgresql is in the allowed listsudo firewall-cmd --list-servicesExpected output (yours may list other services too):
cockpit dhcpv6-client postgresql sshGate 2: listen_addresses. Still on suta-linux-lab-1, as student:
# Switch to the postgres user (type exit to come back)sudo su - postgresNow as postgres on suta-linux-lab-1:
# Keep a copy of the original before you editcp $PGDATA/postgresql.conf $PGDATA/postgresql.conf.orig# Open the main configuration file in vivi $PGDATA/postgresql.confIn vi:
- Type
/listen_addressesand press Enter. The cursor jumps to the line#listen_addresses = 'localhost'. - Press
0(zero) to move to the start of the line. - Press
C(capital C). This deletes the whole line from the cursor and puts you in insert mode. - Type
listen_addresses = '*' - Press Esc.
- Type
:wqand 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.
# Check the new line (-n shows the line number)grep -n '^listen_addresses' $PGDATA/postgresql.confExpected output (your line number may differ):
60:listen_addresses = '*'Gate 3: pg_hba.conf. Still as postgres on suta-linux-lab-1:
# Keep a copy of the original before you editcp $PGDATA/pg_hba.conf $PGDATA/pg_hba.conf.orig# Open the client access file in vivi $PGDATA/pg_hba.confIn vi:
- Press
G(capital G) to jump to the last line. - Press
o(lowercase o) to open a new empty line below it, in insert mode. - Type this line, with your client machine's IP address in place of
CLIENT_IP:host appdb appuser CLIENT_IP/32 scram-sha-256 - Press Esc.
- Type
:wqand 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.
# Return to the student userexitApply the changes. listen_addresses needs a restart. On suta-linux-lab-1, as student:
# Restart PostgreSQL so it re-reads listen_addresses and pg_hba.confsudo systemctl restart postgresql-18# Confirm PostgreSQL now listens on all addresses, port 5432sudo ss -tlnp | grep 5432sslists network sockets.-tTCP,-llistening,-nnumbers instead of names,-pshow the program.| grep 5432keeps 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:
# Connect over the network as appuser to appdb (enter the password when asked)psql -h SERVER_IP -U appuser -d appdb-his the server's address (host). ReplaceSERVER_IPwith the private IP address of suta-linux-lab-1.-Uis the role to connect as.-dis the database.
Inside psql, as appuser (this line is SQL, copy it exactly):
-- Show who you are connected as, and the server address you reachedSELECT 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.