debian

Debian PostgreSQL网络配置

小樊
46
2025-11-09 14:28:40
栏目: 云计算

Debian PostgreSQL Network Configuration Guide

Configuring network access for PostgreSQL on Debian involves adjusting server settings, managing firewall rules, and ensuring secure connectivity. Below is a structured guide to help you set up PostgreSQL for both local and remote access.

1. Install PostgreSQL

Before configuring network settings, ensure PostgreSQL is installed on your Debian system. Run the following commands to update your package list and install PostgreSQL along with its contrib utilities:

sudo apt update
sudo apt install postgresql postgresql-contrib

This installs the latest version of PostgreSQL available in Debian’s repositories.

2. Configure postgresql.conf for Network Listening

The postgresql.conf file controls PostgreSQL’s network behavior. You need to modify two key parameters to allow remote connections:

Steps:

  1. Open the configuration file in a text editor (replace <version> with your PostgreSQL version, e.g., 17):
    sudo nano /etc/postgresql/<version>/main/postgresql.conf
    
  2. Locate the listen_addresses line and update it:
    listen_addresses = '*'
    
  3. Verify the port setting (default is fine for most setups):
    port = 5432
    
  4. Save the file and exit the editor.

3. Adjust pg_hba.conf for Client Authentication

The pg_hba.conf file defines which clients can connect to PostgreSQL and the authentication methods they can use. To allow remote connections, add a rule granting access to all IP addresses (or a specific subnet):

Steps:

  1. Open the authentication file:
    sudo nano /etc/postgresql/<version>/main/pg_hba.conf
    
  2. Add the following line at the end of the file (replace 0.0.0.0/0 with a specific subnet like 192.168.1.0/24 for better security):
    host    all             all             0.0.0.0/0               md5
    
    This rule allows any IP address (0.0.0.0/0) to connect to all databases (all) using MD5 password authentication.

4. Restart PostgreSQL to Apply Changes

After modifying the configuration files, restart the PostgreSQL service to load the new settings:

sudo systemctl restart postgresql

Verify the service status to ensure it’s running without errors:

sudo systemctl status postgresql

5. Configure the Firewall

If your Debian system uses UFW (Uncomplicated Firewall), allow incoming traffic on PostgreSQL’s default port (5432/tcp). For more granular control, restrict access to specific IP addresses.

Allow All Traffic on Port 5432:

sudo ufw allow 5432/tcp

Restrict Access to a Specific IP (e.g., 192.168.1.100):

sudo ufw allow from 192.168.1.100 to any port 5432 proto tcp

Check Firewall Status:

sudo ufw status

Ensure the rule is listed and active.

6. Test Remote Connectivity

Use the psql command-line tool to verify that remote connections work. Replace your_server_ip, your_username, and your_database with your actual server details:

psql -h your_server_ip -p 5432 -U your_username -d your_database

Enter the password for your_username when prompted. If the connection succeeds, you’ll see the PostgreSQL prompt (your_database=>).

7. Optional: Enhance Security with SSL/TLS

For encrypted data transmission between clients and the server, configure SSL/TLS in PostgreSQL.

Generate SSL Certificates:

  1. Create a directory for certificates:
    mkdir -p /etc/postgresql/<version>/main/ssl
    
  2. Generate a self-signed certificate (valid for 365 days):
    openssl req -new -x509 -days 365 -nodes -text -subj "/CN=postgres" -out /etc/postgresql/<version>/main/ssl/server.crt -keyout /etc/postgresql/<version>/main/ssl/server.key
    
  3. Set strict permissions on the private key:
    chmod 600 /etc/postgresql/<version>/main/ssl/server.key
    

Configure PostgreSQL for SSL:

  1. Open postgresql.conf again:
    sudo nano /etc/postgresql/<version>/main/postgresql.conf
    
  2. Enable SSL and specify certificate paths:
    ssl = on
    ssl_cert_file = '/etc/postgresql/<version>/main/ssl/server.crt'
    ssl_key_file = '/etc/postgresql/<version>/main/ssl/server.key'
    
  3. Restart PostgreSQL to apply SSL settings:
    sudo systemctl restart postgresql
    

Verify SSL Connection: Connect to PostgreSQL using the --ssl flag:

psql "host=your_server_ip port=5432 dbname=your_database user=your_username sslmode=require"

Key Notes for Production Environments

0
看了该问题的人还看了