Connecting to a remote PostgreSQL database can prove daunting for some teams. Your organization risks losing valuable time, which then leads to lost productivity. Thankfully, there are four different ways to connect to a remote PostgreSQL database and improve your team's efficiency.
Before trying to access a remote PostgreSQL database, confirm that:
The default setting for PostgreSQL allows only localhost connections. However, you can adjust a few configuration files to enable external or remote connections.
This adjustment enables connections beyond the local machine:
This update controls who (users, IP addresses) can access the database by requiring password authentication (md5).
For IPV4 addresses: host all all 0.0.0.0/0 md5
For IPV6 addresses: host all all ::0/0 md5
Allowing remote connections to access a PostgreSQL server can expose it to threat actors and other security risks. As such, you should take proactive measures to protect your network and database.
Firewalls act as a barrier that restricts external access. As such, you must configure the server’s firewall to allow traffic on its default port, usually port 5432.
For a better illustration, let’s use an example in which you’re running PostgreSQL on a Linux server. To configure the firewall and permit traffic on port 5432, you can use ufw by running the following command:
sudo ufw allow 5432/tcp
You can also use a tool like iptables by running the following command:
iptables -A INPUT -p tcp -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 the localhost. With a VPN, the database isn't directly exposed to the public internet.
On the other hand, SSH tunnels forward traffic from a local port on your machine to the PostgreSQL server over an encrypted connection. This helps protect data during transmission.
Use the following command to establish an SSH tunnel:
ssh -L 5432:localhost:5432 user@remote_host
After establishing the required configurations and ensuring network security, you can now connect to your remote PostgreSQL database.
This method leverages the following command syntax:
psql -h
-p
-U
-d
When you press "Enter," you'll receive a prompt to enter your password for authentication.
Here's how to use various programming languages to connect to a remote PostgreSQL database.
import psycopg2
conn = psycopg2.connect(
host="remote_host",
database="db_name",
user="username",
password="password"
)
const { Client } = require('pg');
const client = new Client({
host: 'remote_host',
database: 'db_name',
user: 'username',
password: 'password',
});
client.connect();
Connection connection = DriverManager.getConnection(
"jdbc:postgresql://remote_host:5432/db_name", "username", "password");
To connect to PostgreSQL, you need the PostgreSQL JDBC driver. Therefore, include it as a dependency in your project.
Using Gradle, add this to your build.gradle file:
implementation 'org.postgresql:postgresql:42.6.0' // Use the latest version
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 instance, for incorrect client authentication configuration:
The most straightforward way to secure remote PostgreSQL connections is by using complex passwords. Additionally, you can:
Delinea can help secure your databases by centralizing management. You get:
Book a demo today to see Delinea in action!