Connecting to a remote PostgreSQL database can prove daunting for some teams. Your organization risks losing valuable time, which then leads to lost productivity.
There are four methods for connecting to a remote PostgreSQL database: psql, pgAdmin, a third-party client such as DBeaver, or a driver in your application code. All four need the same two configuration changes on the server first, and all four carry the same risk if you make those changes as most tutorials describe.
Before you start, confirm you have:
PostgreSQL accepts only localhost connections by default. Two configuration files change that.
This adjustment enables connections beyond the local machine:
The pg_hba.conf file controls which users can connect, from which addresses and with which authentication method.
# IPv4: one application subnet, not the whole internet
host all all 10.0.4.0/24 scram-sha-256
# IPv6: the equivalent scoped prefix
host all all 2001:db8:4::/64 scram-sha-256
Changing the method in pg_hba.conf does not re-hash existing passwords. Set password_encryption = 'scram-sha-256' in postgresql.conf, restart, then have each user set their password again so the new hash is stored. Skip that step and those users can no longer log in.
Allowing remote connections to a PostgreSQL server exposes it to anyone who can route to port 5432. Three controls narrow that exposure.
Configure the server's firewall to allow traffic on PostgreSQL's port, 5432 by default, and only from the addresses that need it.
On a Linux server, ufw does this:
sudo ufw allow from 10.0.4.0/24 to any port 5432 proto tcp
Or with iptables:
iptables -A INPUT -p tcp -s 10.0.4.0/24 -m state --state NEW --dport 5432 -j ACCEPT
For cloud environments, you can adjust the security group settings or firewall rules within the cloud platform's management console.
Using a VPN, you can keep PostgreSQL's default firewall settings and even limit its "listen_addresses" to localhost. With a VPN, the database isn't directly exposed to the public internet.
SSH tunnels forward traffic from a local port on your machine to the PostgreSQL server over an encrypted connection, which protects data in transit.
Open the tunnel:
ssh -L 5432:localhost:5432 user@remote_host
Then connect to localhost on port 15432. The published example used 5432 on both sides, which fails when PostgreSQL is already running locally on that port.
With the server configured and the network restricted, connect using whichever method fits your work.
psql takes the connection details as flags:
psql -h <host> -p <port> -U <username> -d <database>
When you press Enter, you'll receive a prompt to enter your password for authentication.
Each driver takes the same connection details. Read the password from the environment rather than writing it into the file:
import psycopg2
conn = psycopg2.connect(
host="remote_host",
database="db_name",
user="username",
password=os.environ["DB_PASSWORD"],
const { Client } = require('pg');
const client = new Client({
host: 'remote_host',
database: 'db_name',
user: 'username',
password: process.env.DB_PASSWORD,
});
client.connect();
Connection connection = DriverManager.getConnection(
"jdbc:postgresql://remote_host:5432/db_name",
"username", System.getenv("DB_PASSWORD"));
To connect to PostgreSQL, you need the PostgreSQL JDBC driver. Therefore, include it as a dependency in your project.
When connecting remotely, you may encounter authentication issues for several reasons, including wrong username/password combinations or incorrect "pg_hba.conf" settings.
If you receive a "connection refused" message, it might be because you haven't configured PostgreSQL to listen to the right IP addresses or your firewall to allow traffic on port 5432.
For an incorrect client authentication configuration:
"host all all 203.0.113.25.32 scram-sha-256
Password complexity does the least work on this list, because a password that never changes and is shared across a team fails whatever its length. These five do more:
Every method above ends the same way: a password typed at a prompt, saved in a client profile or read from a file on a developer's laptop. That password is usually shared, rarely changed and attached to no particular person. None of the network controls above touch it.
That is the gap Delinea Secret Server fills for a PostgreSQL account. Its PostgreSQL password changer lets you:
Least privilege governs what the database role can do. Identity governs who is behind the credential. The information above covers the first. The second needs something outside PostgreSQL.
See how a vaulted database credential works: book a demo today.