debian

debian pgadmin连接远程数据库方法

小樊
41
2025-09-23 06:05:35
栏目: 智能运维

Prerequisites
Ensure PostgreSQL is installed and running on the remote Debian server. You can install it using sudo apt update && sudo apt install postgresql postgresql-contrib, then verify with psql --version. PgAdmin should also be installed on your local machine (Debian or other OS)—install via sudo apt install pgadmin4 if not already available.

Step 1: Configure PostgreSQL to Allow Remote Connections
To permit remote access, modify two critical PostgreSQL configuration files:

  1. Edit postgresql.conf:
    Open the file in a text editor (e.g., sudo nano /etc/postgresql/<version>/main/postgresql.conf). Locate the line starting with listen_addresses and change it from localhost (or a specific IP) to *—this allows PostgreSQL to accept connections from any IP address.
    Example:

    listen_addresses = '*'
    

    Save and close the file.

  2. Edit pg_hba.conf:
    Open the authentication file (sudo nano /etc/postgresql/<version>/main/pg_hba.conf). Add the following line at the end to allow MD5-encrypted password connections from any IP (0.0.0.0/0):

    host all all 0.0.0.0/0 md5
    

    For stricter security, replace 0.0.0.0/0 with a specific IP range (e.g., 192.168.1.0/24 for your local network). Save and close.

  3. Restart PostgreSQL:
    Apply changes by restarting the service:

    sudo systemctl restart postgresql
    

Step 2: Configure the Firewall
If your Debian server uses ufw (Uncomplicated Firewall), allow traffic on PostgreSQL’s default port (5432):

sudo ufw allow 5432/tcp

Enable the firewall if it’s not active:

sudo ufw enable

Verify the rule is in place:

sudo ufw status

Look for a line like 5432/tcp ALLOW Anywhere.

Step 3: Set Up the Remote Connection in PgAdmin

  1. Open PgAdmin:
    Launch the application from your application menu or terminal (pgadmin4).

  2. Add a New Server:
    Right-click the “Servers” node in the left-hand navigation pane and select Create > Server….

  3. Configure Connection Details:

    • General Tab: Enter a descriptive name for the connection (e.g., “Remote PostgreSQL Server”).
    • Connection Tab:
      • Host: Input the remote server’s IP address or hostname (e.g., 192.168.1.100).
      • Port: Use the default PostgreSQL port (5432) unless modified.
      • Authentication: Choose “Standard” and enter your PostgreSQL username/password (e.g., postgres/your_password).
      • Database: Optionally specify a default database (leave blank to list all databases after connection).
  4. Save and Test:
    Click Save to store the configuration. Double-click the new server entry to initiate the connection. If successful, PgAdmin will display the server’s databases in the left-hand pane.

Optional: Enhance Security with SSL
For encrypted connections, configure SSL on the PostgreSQL server and update PgAdmin settings:

  1. Generate SSL Certificates:
    On the remote server, run:

    mkdir -p /etc/postgresql/<version>/main/ssl
    cd /etc/postgresql/<version>/main/ssl
    openssl req -new -x509 -days 365 -nodes -text -subj "/CN=postgres" -out server.crt -keyout server.key
    chmod 600 server.key
    
  2. Configure PostgreSQL for SSL:
    Edit postgresql.conf again and add/update these lines:

    ssl = on
    ssl_cert_file = '/etc/postgresql/<version>/main/ssl/server.crt'
    ssl_key_file = '/etc/postgresql/<version>/main/ssl/server.key'
    

    Restart PostgreSQL:

    sudo systemctl restart postgresql
    
  3. Update PgAdmin Connection:
    In the PgAdmin connection settings (Step 3), go to the SSL Tab and set SSL Mode to require (or verify-full for strict certificate validation). PgAdmin will now use SSL for all data transmitted to/from the remote server.

Troubleshooting Common Issues

0
看了该问题的人还看了