Question: What port does PostgreSQL use for replication?

Answer

PostgreSQL uses the same port for both standard client connections and replication connections, which by default is port 5432. When setting up replication, you don't need to configure a separate port specifically for replication purposes. Instead, the distinction between a normal connection and a replication connection is made through the connection string or the settings in the pg_hba.conf file.

Example: Replication Connection String

When a replication client connects to the PostgreSQL server, it specifies the desire to replicate by including replication-related parameters in its connection string. Here's an example connection string for a replication connection:

host=replica-server.example.com port=5432 user=replicator password=yourpassword dbname=replication application_name=my_replica_client sslmode=prefer

In this string, dbname=replication signifies that this is a replication connection. Note that sslmode=prefer is optional and used here to indicate a preference for SSL connections.

pg_hba.conf Configuration

For replication to work, the PostgreSQL instance must be configured to allow replication connections. This is done in the pg_hba.conf configuration file. You would add a line similar to the following:

host replication replicator 192.168.1.100/32 md5

This line allows a host with the IP 192.168.1.100 to connect for replication purposes using the user replicator and requires MD5 password authentication.

Summary

The key takeaway is that PostgreSQL doesn't use a separate port for replication. Both regular and replication connections can be made through the server's main port, typically 5432, unless it has been changed in the PostgreSQL configuration. The type of connection is determined not by the port number but by the connection parameters and the server's configuration.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.