If you have a PostgreSQL database instance running on an Ubuntu server, you might want
to access it from outside the server.
In this article, I'm going to show you how you can do it.
The default Postgres connection port is usually 5432. We are going to expose our connection through this port.
You can use your other port if not using the default one.
Allow Postgres port in the firewall (
5432 in our case)
sudo ufw allow 5432/tcp
Open your Postgres config file
sudo nano /etc/postgresql/<postgres_version>/main/pg_hba.conf
Notice that I used ../14/.. . That's because I have PostgreSQL version 14 installed. Change it to your installed version
It will open your PostgreSQL config file.
By default, the allowed address to access the Postgres connection is
That means you can only be connected if you are on the same server.
We have to change that since we want the connection to be public.
Change the IP address from
What we did is allow any IP address at any port to access ur connection.
Now you can connect to the database from anywhere.
N.B: Changing from
127.0.0.1/32 in your configs to
0.0.0.0/0 means you are allowing any device from any.
This is insecure. If you want to be more secure, you can change it from
Construct a connection string like this and connect it to your database.
You can also test the connection with PGAdmin which is a powerful Postgres client.
You should be able to access your database 🎉