PostgreSQL Remote Connection Configuration: Solving the Critical listen_addresses Setting

Nov 22, 2025 · Programming · 9 views · 7.8

Keywords: PostgreSQL | remote connection | listen_addresses | pg_hba.conf | network configuration

Abstract: This article provides a comprehensive guide to configuring PostgreSQL for remote connections, focusing on the crucial role of the listen_addresses parameter in postgresql.conf. Through practical case analysis, it explains common connection errors and offers complete solutions including pg_hba.conf configuration, firewall settings, and network verification, with connection examples in Python and Node.js.

Introduction

PostgreSQL, as an enterprise-grade open-source relational database, often requires remote connection support in practical deployments. However, by default, PostgreSQL only allows local connections, requiring multiple configuration adjustments to achieve secure remote access.

Core Configuration Issue Analysis

When connecting to PostgreSQL remotely, the most common error message is <span style="font-family: monospace;">"Is the server running on host and accepting TCP/IP connections on port 5432?"</span>. This error typically indicates that the client cannot establish a TCP connection to the server, which may involve multiple layers of issues.

Key Configuration: listen_addresses Parameter

The network listening behavior of the PostgreSQL server is controlled by the <span style="font-family: monospace;">listen_addresses</span> parameter in the <span style="font-family: monospace;">postgresql.conf</span> file. The default value is <span style="font-family: monospace;">'localhost'</span>, meaning the server only accepts connection requests from the local loopback address.

To enable remote connections, this parameter must be modified:

# Set in postgresql.conf file
listen_addresses = '*'  # Listen on all network interfaces

Or specify particular IP addresses:

listen_addresses = '192.168.1.100,192.168.1.101'  # Listen on specific IP addresses

After modification, restart the PostgreSQL service for the changes to take effect:

sudo systemctl restart postgresql

Client Authentication Configuration

The <span style="font-family: monospace;">pg_hba.conf</span> file controls client connection authentication rules. Correct configuration examples include:

# Allow trust connections from specific IP address
host all all 192.168.1.50/32 trust

# Allow subnet connections with password authentication
host all all 192.168.1.0/24 md5

# Allow connections from all IPs (use with caution in production)
host all all 0.0.0.0/0 md5

After configuration, the PostgreSQL service must be restarted.

Network Layer Configuration

Firewall Settings

Ensure the server firewall allows inbound connections on PostgreSQL's default port 5432:

# Using ufw
sudo ufw allow 5432/tcp

# Using iptables
iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

Network Connectivity Verification

Use telnet or nc commands on the client to test network connectivity:

telnet server_ip 5432
# or
nc -zv server_ip 5432

Connection Command Details

Basic syntax for connecting to a remote PostgreSQL instance using the psql client:

psql -h <hostname> -p <port> -U <username> -d <database> -W

Parameter explanation:

Practical usage example:

psql -h 192.168.1.100 -p 5432 -U postgres -d mydatabase -W

Programming Language Connection Examples

Python Connection

import psycopg2

try:
    conn = psycopg2.connect(
        host="192.168.1.100",
        port=5432,
        database="mydatabase",
        user="myuser",
        password="mypassword"
    )
    print("Connection successful")
    conn.close()
except Exception as e:
    print(f"Connection failed: {e}")

Node.js Connection

const { Client } = require('pg');

const client = new Client({
    host: '192.168.1.100',
    port: 5432,
    database: 'mydatabase',
    user: 'myuser',
    password: 'mypassword'
});

client.connect()
    .then(() => console.log('Connection successful'))
    .catch(err => console.error('Connection failed:', err))
    .finally(() => client.end());

Security Best Practices

When configuring remote connections in production environments, follow these security principles:

Troubleshooting Guide

When encountering connection issues, troubleshoot using these steps:

  1. Verify PostgreSQL service status on the server
  2. Check <span style="font-family: monospace;">listen_addresses</span> configuration
  3. Confirm <span style="font-family: monospace;">pg_hba.conf</span> rules
  4. Test network connectivity and firewall settings
  5. Validate client authentication information

Conclusion

Configuring PostgreSQL for remote connections involves coordination across multiple layers, with the <span style="font-family: monospace;">listen_addresses</span> parameter setting being the critical first step. Through systematic configuration and strict security controls, both convenient and secure remote database access can be achieved.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.