Loading...

We've detected that your browser language is Chinese. Would you like to visit our Chinese website? [ Dismiss ]
By: Emma

What Is a PostgreSQL Replication User

A replication user is a dedicated database role created with the REPLICATION attribute. This lets the role connect in replication mode and stream Write-Ahead Logs (WAL) through the physical or logical replication protocol, instead of running SQL queries against tables like a standard LOGIN role.

Teams create a separate replication role rather than using the postgres superuser to follow the principle of least privilege. This distinction matters for security:

  • The postgres superuser can read, modify, or drop any object in the database, making its credentials highly sensitive.
  • If a standby server is compromised, exposed superuser credentials hand over full control of the primary database.
  • A restricted replication user can only stream WAL files, limiting the blast radius of a leaked credential.

A common point of confusion is the CREATEROLE privilege. As PostgreSQL 18 documentation confirms, holding CREATEROLE does not let a role create replication users, nor does it allow granting or revoking the REPLICATION privilege for others. Only true superusers, or roles already granted REPLICATION themselves, can provision new replication accounts.

create replication user postgre

Prerequisites Before You Create the User in PostgreSQL

Before creating a replication user, a few parameters in postgresql.conf need to be set correctly. Skipping these will prevent the standby server from establishing a replication stream.

Check the following on the primary database instance:

  • WAL level: Set wal_level to replica, or logical if you plan to use logical replication instead of streaming physical replication. This controls how much detail gets written to the Write-Ahead Logs.
  • Replication senders: Set max_wal_senders high enough to cover every concurrent replication connection you plan to run. Each standby server or backup tool uses one sender slot.
  • Replication slots: Make sure max_replication_slots has room for a dedicated slot per standby server. Slots keep a replica from falling too far behind by tracking its current log position.
  • Administrative privileges: Creating the user requires a superuser connection, or a role with CREATEROLE plus an explicit replication grant.

How to Create the Replication User (3 Steps)

Setting up a replication role involves two layers: the role itself inside PostgreSQL, and the authentication rule that lets it connect over the network. Follow these three steps on the primary server.

Step 1: Create the user in PostgreSQL

Connect to the primary instance as the postgres superuser and run the creation command. Give it a strong password, along with the REPLICATION attribute so it can stream WAL.

bash
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'your_secure_password';

CREATE USER includes LOGIN by default, so there’s no need to add it separately. If you use CREATE ROLE instead, add LOGIN explicitly, since CREATE ROLE does not assume it.

Step 2: Update host-based authentication in pg_hba.conf

Creating the role inside PostgreSQL is not enough. The server also needs to be told to accept the connection. Open pg_hba.conf and add a line scoped to the standby server’s IP address.

bash
host replication replicator 192.168.1.50/32 scram-sha-256

As of PostgreSQL 14, scram-sha-256 is the default authentication method and should be used instead of the older, weaker md5 method. The database field is set to the literal value replication, which tells PostgreSQL this rule applies to replication connections rather than regular database access.

Step 3: Reload the database configuration

Changes to pg_hba.conf take effect on reload, so a full restart isn’t needed. Reload from inside psql:

bash
SELECT pg_reload_conf();

Or from the system terminal:

bash
pg_ctl reload -D /var/lib/postgresql/data

Verify the User Was Created Correctly

Once reloaded, confirm the replication user exists with the correct attributes.

In psql, run \du to list all roles. Look for replicator and check that Replication appears under Attributes. You can also query the catalog directly:

bash
SELECT rolname, rolreplication, rollogin FROM pg_roles WHERE rolname = 'replicator';

To check that the pg_hba.conf line was parsed without errors, query pg_hba_file_rules:

bash
SELECT line_number, type, database, user_name, address, auth_method, error 
FROM pg_hba_file_rules 
WHERE user_name = 'replicator';

A NULL value in the error column means PostgreSQL parsed the new rule successfully.

If You’re on AWS RDS, Azure, or Google Cloud SQL

Managed PostgreSQL services don’t give you shell access, so pg_hba.conf and postgresql.conf can’t be edited directly. Each platform grants replication privileges through its own system role instead of raw superuser access.

Managed Platform Configuration Method Default System Role How Network Access Is Scoped
AWS RDS / Aurora SQL grant rds_replication Security groups / VPC peering
Azure Database SQL grant azure_pg_admin Connection security / firewall rules
Google Cloud SQL SQL creation + console cloudsqlsuperuser Authorized networks / Cloud SQL Auth Proxy

Amazon RDS and Aurora: Grant the built-in role to enable replication:

bash
GRANT rds_replication TO your_replication_user;

This works without superuser access, and it’s also the foundation that automatic failover in RDS relies on to promote a standby when the primary goes down.

Azure Database for PostgreSQL (Flexible Server): Grant membership in Azure’s administrative role:

bash
GRANT azure_pg_admin TO your_replication_user;

You can also assign REPLICATION directly if you’re connected as the primary administrator account.

Google Cloud SQL: Create the user under Cloud SQL’s elevated management role:

bash
CREATE USER replicator WITH REPLICATION IN ROLE cloudsqlsuperuser LOGIN PASSWORD 'your_secure_password';

Manage network access through the Google Cloud Console or gcloud, or use the Cloud SQL Auth Proxy to skip IP allowlisting entirely.

Common Errors and Fixes When Creating Replication User

Even with the right steps, setting up replication can still trigger authentication or authorization errors. Here’s how to resolve the ones you’re most likely to run into.

Error: “permission denied to create role”

This means you’re not connected as a superuser, or you’re using a role with CREATEROLE that hasn’t been explicitly granted replication rights.

Connect as the postgres superuser or another superuser role. If you need to use a non-superuser with CREATEROLE, remember that this privilege alone doesn’t grant the ability to create or manage REPLICATION roles. A superuser has to delegate that authority separately.

Error: “FATAL: no pg_hba.conf entry for replication connection”

The primary’s authentication file is blocking the standby’s connection, usually because a rule is missing, has a typo, sits in the wrong order, or the config was never reloaded.

Check pg_hba.conf and confirm a rule matches the standby’s IP address, the user name, and the literal database value replication. After fixing the line, run SELECT pg_reload_conf(); on the primary, then query pg_hba_file_rules to confirm there are no parsing errors.

Error: “password authentication failed”

This is usually a hashing mismatch: the password was stored using the older md5 format, but pg_hba.conf requires scram-sha-256, or the reverse.

Check the active encryption setting with SHOW password_encryption;. If it’s not set to scram-sha-256, update it, then re-run ALTER USER replicator WITH PASSWORD 'your_password'; to re-hash the password under the new setting. Make sure the matching pg_hba.conf line also uses scram-sha-256.

Error: “pg_basebackup: permission denied”

Despite the wording, this is rarely about database roles or replication privileges. It’s usually a filesystem issue, where the operating system user running the command doesn’t have write access to the target directory, or files in the primary’s data directory have the wrong ownership.

Make sure the destination directory on the standby is empty, then set the correct owner:

bash
chown -R postgres:postgres /var/lib/postgresql/data

Adjust the path for the actual deployment. Also check that no stray lock files or external tools are holding a lock on the primary’s data directory.

Automated Replication for Near-Zero RPO High Availability

Setting up a replication user is only the first step. Keeping that replication stream healthy, monitored, and ready to fail over when the primary database goes down is a separate challenge, and one that manual PostgreSQL configuration doesn’t fully solve on its own.

i2Availability is built for exactly this gap. It provides byte-level real-time data replication between a production database and its standby, so changes on the primary are captured and sent to the backup continuously rather than on a schedule. This keeps RPO close to zero, since the standby is never far behind the source.

Key features include:

  • Automatic failure detection: Heartbeat monitoring checks server hardware, network status, and application health, so a failing primary is caught quickly rather than discovered after the fact.
  • Sub-second failover: When a failure is confirmed, the standby takes over automatically using pre-set procedures and virtual IP drift, without requiring manual intervention.
  • Zero-delay replication: Byte-level replication captures every write operation on the production system, so the backup can be used directly without a lengthy restoration step.
  • Arbitration mechanism: Node and disk arbitration prevent split-brain scenarios where both servers mistakenly believe they should be active at the same time.
  • Cross-platform support: Works across physical, virtual, and cloud environments, including hybrid deployments that combine on-premises databases with public cloud standbys.

The choice between synchronous and asynchronous replication affects how much lag a standby can tolerate before failover becomes risky, and i2Availability’s real-time replication is designed to keep that gap as small as possible regardless of which mode fits the environment.

For teams also managing full-machine or file-based replication alongside their database, i2Migration extends similar real-time synchronization to broader migration and disaster recovery scenarios.

FREE Trial for 60-Day

FAQ

Q1: Is postgres the same as a replication user?

No. The postgres superuser bypasses all permission checks, including replication, but it’s meant for administrative tasks, not for streaming WAL. A dedicated replication user with only the REPLICATION attribute limits what a compromised credential can access.

 

Q2: Do I need to restart PostgreSQL, or is reload enough?

A reload is enough for changes to pg_hba.conf. Run SELECT pg_reload_conf(); or pg_ctl reload, and the new authentication rule takes effect without dropping existing connections.

 

Q3: CREATE ROLE vs CREATE USER, is there a difference?

CREATE USER is equivalent to CREATE ROLE, except it includes LOGIN by default. CREATE ROLE does not, so you need to add LOGIN explicitly if the role should be able to connect.

 

Q4: Can I use md5 instead of scram-sha-256?

Yes, but it’s not recommended. PostgreSQL has defaulted to scram-sha-256 since version 14 because it’s more resistant to password sniffing. Use md5 only if an older client can’t support scram-sha-256.

Conclusion

Creating a PostgreSQL replication user is straightforward once the role, authentication, and network layers are configured correctly. Getting the REPLICATION attribute, pg_hba.conf entry, and password hashing method aligned from the start avoids most of the errors covered in this guide.

For teams running PostgreSQL alongside other databases and virtualized systems, keeping replication healthy at scale often calls for more automation than manual configuration alone. Info2soft builds tools that extend this kind of real-time protection across broader infrastructure, from database replication to full-system high availability.

Emma is the bridge between complex engineering and the people who need it. As a content creator at Info2soft, she spends her days translating "tech-speak" into clear, actionable stories about data resilience. She’s not just documenting software; she's uncovering how data replication and recovery actually change the way businesses run.

More Related Articles

Ready to Enhance Business Data Security?

· Enterprise & Mid-market Customers Worldwide

· Support team available to assist you throughout your trial

· Start a 60-day free trial or view demo to see how Info2Soft protects enterprise data.

Please fill out the form and submit it, our customer service representative will contact you soon.
By submitting this form, I confirm that I have read and agree to the Privacy Notice.
{{ isSubmitting ? 'Submitting...' : 'Submit' }}