/ Database

How to access Postgres from VPS on pgAdmin

Blog Image
Creator Image
Bashar Alshaibani
18 Aug 2024 -
8 min Reading time

To access your PostgreSQL server database using pgAdmin, follow these steps:

1. Ensure PostgreSQL is Configured to Accept Remote Connections

By default, PostgreSQL might only be configured to accept connections from localhost. To connect remotely using pgAdmin, you'll need to adjust some configuration files.

a. Edit the postgresql.conf File

  1. SSH into your VPS where PostgreSQL is installed.

    ssh username@your_vps_ip
    
  2. Open the postgresql.conf file in a text editor (like nano or vim).

    sudo nano /etc/postgresql/14/main/postgresql.conf
    
    • The exact path may vary depending on your PostgreSQL version and system configuration.
  3. Look for the listen_addresses setting and change it to accept connections from any IP address:

    listen_addresses = '*'
    • By default, it might be set to localhost. Change it to * to allow all incoming connections, or specify a particular IP address to restrict access.
  4. Save and exit the file (in nano, you can press CTRL + X, then Y, and then Enter).

b. Edit the pg_hba.conf File

  1. Open the pg_hba.conf file:

    sudo nano /etc/postgresql/14/main/pg_hba.conf
    
  2. Add a new line to allow remote connections. Add this line at the end of the file:

    host all all 0.0.0.0/0 md5
    • This line allows any IP address (0.0.0.0/0) to connect to the database using a password (md5).
    • If you want to restrict access to specific IP addresses, replace 0.0.0.0/0 with the desired IP address or range.
  3. Save and exit the file.

c. Restart PostgreSQL

After making changes to the configuration files, restart PostgreSQL to apply them:

sudo systemctl restart postgresql

2. Set Up Firewall Rules (If Necessary)

Ensure your VPS firewall allows incoming connections on the PostgreSQL port (default is 5432).

  1. Allow PostgreSQL through the firewall:

    sudo ufw allow 5432/tcp
    
  2. Check the firewall status:

    sudo ufw status
    

    Ensure that the PostgreSQL port is open.

3. Set Up pgAdmin on Your Local Machine

If you don't have pgAdmin installed, download and install it from the official website:

4. Connect to Your PostgreSQL Server Using pgAdmin

  1. Open pgAdmin.

  2. Create a new server connection:

    • In the pgAdmin interface, right-click on "Servers" in the left-hand menu and select "Create" > "Server".
  3. Configure the connection:

    • General Tab:
      • Name your connection (e.g., "My VPS Database").
    • Connection Tab:
      • Host name/address: Enter your VPS IP address.
      • Port: Enter 5432 (or another port if you've configured PostgreSQL to use a different one).
      • Maintenance database: Enter postgres (or another database if you prefer).
      • Username: Enter your PostgreSQL username (e.g., postgres).
      • Password: Enter the password for the PostgreSQL user.
      • Save password: Check this box if you want pgAdmin to remember the password.
  4. Test the connection:

    • Click the "Save" button. If everything is configured correctly, pgAdmin should connect to your PostgreSQL server, and you’ll see the server listed under "Servers" in pgAdmin.
  5. Access the Databases:

    • Once connected, expand the server node to view the databases, schemas, tables, etc.

5. Troubleshooting

If you encounter any issues:

  • Firewall: Double-check that your firewall settings allow traffic on port 5432.
  • PostgreSQL Configuration: Ensure that listen_addresses is set correctly and that the pg_hba.conf file has the appropriate entries.
  • Network Issues: Ensure your local machine can reach the VPS (e.g., check if you can ping the VPS IP address).
  • pgAdmin Logs: Check pgAdmin’s error logs for more detailed information if the connection fails.

This setup should allow you to manage your PostgreSQL databases on your VPS using pgAdmin from your local machine.