Skip to main content
Setting Up and Connecting to PostgreSQL on a VPS - Virtarix Blog

Setting Up and Connecting to PostgreSQL on a VPS

December 13, 2024 · Blog / Technical Guides

This guide provides a comprehensive process for setting up a PostgreSQL database on a Virtual Private Server (VPS) and remotely connecting to it using pgAdmin. It is specifically tailored for Ubuntu 22.04 LTS (Jammy Jellyfish) or other Debian-based systems that use the apt package manager. Commands and configurations provided in this guide may differ for other Linux distributions.

The tutorial covers updating the system, configuring PostgreSQL settings, securing connections, and testing the setup to ensure a reliable and secure database environment.

Step 1: update the system

Begin by updating the system. Open your terminal and run the following commands to update the package lists and upgrade installed packages:

Update and upgrade Ubuntu packages
sudo apt update && sudo apt upgrade

Step 2: install PostgreSQL

Install PostgreSQL by entering:

Install PostgreSQL from APT
sudo apt install postgresql

Ensure you have the latest version by running:

Refresh the APT package index
sudo apt update

Next, modify the postgresql.conf file to allow connections from external addresses. Use the command:

Open the PostgreSQL server configuration
sudo nano /etc/postgresql/*/main/postgresql.conf

Terminal command opening postgresql.conf in Nano

Locate the section titled Connections and Authentication and find the line containing listen_addresses. Change this line as follows:

Listen for PostgreSQL connections on all addresses
listen_addresses = '*'

This change allows the server to accept connections from any address. Save your changes and exit the editor.

PostgreSQL connection settings displayed in postgresql.conf within Nano

Step 3: log in as the PostgreSQL user

Log in as the PostgreSQL with the command:

Open psql as the postgres system user
sudo -u postgres psql

Once logged in, update the PostgreSQL user with a new secure password by executing:

Set the postgres account password
ALTER USER postgres WITH ENCRYPTED PASSWORD 'yourpassword';

If no error appears, the password update was successful. Exit the PostgreSQL shell by typing:

Exit the psql shell
\q

PostgreSQL psql session setting an encrypted password for the postgres user

Step 4: edit the pg_hba.conf file

Edit the pg_hba.conf file to define the authentication methods for remote connections. Use the command

Open the PostgreSQL client authentication rules
sudo nano /etc/postgresql/*/main/pg_hba.conf

Client authentication rules displayed in pg_hba.conf within Nano

Add the following entries only in an isolated test environment. They expose PostgreSQL authentication to every IPv4 and IPv6 address. For production, replace both networks with trusted client ranges, require hostssl, use a firewall or private network, verify certificate handling, and test access before removing any existing rule.

Allow remote PostgreSQL clients from all addresses
## Allow remote connections
host    all             all             0.0.0.0/0            scram-sha-256
hostssl all             all             ::/0                 scram-sha-256

Step 5: restart PostgreSQL service

Restart the PostgreSQL service to apply the changes:

Restart the PostgreSQL service
sudo systemctl restart postgresql.service

Step 6: configure the firewall

Allow PostgreSQL-related traffic through the VPS firewall. Run the following command:

Allow PostgreSQL through UFW
sudo ufw allow 5432

Ensure unauthorized IP addresses are blocked and only trusted ones are allowed.

Step 7: test the connection

Install the PostgreSQL client if it's not already installed:

Install the PostgreSQL command-line client
sudo apt install postgresql-client

Terminal installing the PostgreSQL client after restarting the service

To test the connection, run:

Connect to a remote PostgreSQL server with psql
psql --host YOUR_IP_ADDRESS --username postgres --password

Replace YOUR_IP_ADDRESS with the IP address of your VPS. Press Enter and provide the password when prompted. If successful, you will access your PostgreSQL database.

Successful remote PostgreSQL TLS connection at the psql prompt

Step 8: connect via pgAdmin4

To connect using pgAdmin4, follow these steps:

  1. Open pgAdmin4 and navigate to Object ➔ Register ➔ Server.
  2. Enter a name for your server in the "General" tab.
  3. In the "Connection" tab, input your VPS’s IP address, username, and password.

Click Save.

You should now be connected to your PostgreSQL database via pgAdmin4. Use the pgAdmin4 interface to manage and create necessary databases.

pgAdmin server registration form requesting a host name or address

Additional notes

  • For production environments, secure your server by restricting IP addresses in the pg_hba.conf file and using a firewall.
  • Regularly update your system and database packages to maintain security.

Enjoy managing your PostgreSQL database securely and efficiently!

Frequently asked questions

How do I restrict access to specific IP addresses for improved security?

You can restrict access by modifying the pg_hba.conf file. Replace the 0.0.0.0/0 entry with a specific IP or range. For example:

Allow PostgreSQL clients from one IPv4 subnet
host    all             all             192.168.1.0/24       scram-sha-256

This limits connections to devices within the 192.168.1.0/24 subnet. Be sure to restart the PostgreSQL service after making changes.

Why can’t I connect to PostgreSQL from pgAdmin4 even after completing all steps?

Common issues include:

Firewall settings

Ensure port 5432 is open using:

Allow PostgreSQL through UFW
sudo ufw allow 5432

Incorrect configuration

Verify that listen_addresses is set to * in postgresql.conf and the appropriate entries exist in pg_hba.conf.

Network restrictions

Check if your VPS provider has additional network security rules blocking the connection.

What should I do if I forget the PostgreSQL password?

You can reset the password by logging in as the postgres system user on your VPS and running the following commands:

Attempt a postgres password reset from the shell
sudo -u postgres psql ALTER USER postgres WITH ENCRYPTED PASSWORD 'newpassword';

Replace newpassword with a strong, unique password. Exit with \q and restart the PostgreSQL service to apply changes.

Ready to run PostgreSQL on Virtarix VPS?

Compare VPS sizes for PostgreSQL databases, app connections, root access, NVMe storage, IPv4 + IPv6, snapshots, backups, and predictable database headroom.

VPS S

For small sites, dev servers and Docker

$ 5 .50 /month
  • 3 cores
  • 6 GB
  • 50 GB NVMe
  • Unlimited
Get It Now
BEST SELLER

VPS M

For growing apps, websites and staging

$ 11 .40 /month
  • 6 cores
  • 16 GB
  • 100 GB NVMe
  • Unlimited
Get It Now
Peter French
About the Author Peter Frenchis the Managing Director at Virtarix, with over 17 years in the tech industry. He has co-founded a cloud storage business, led strategy at a global cloud computing leader, and driven market growth in cybersecurity and data protection.