PostgreSQL external database cluster
On-premises database cluster
OPSWAT recommends setting up an external PosgreSQL cluster in the streaming replication mode as a primary and a hot standby pair.
The setup below assumes that PostgreSQL is installed on Windows.
The server operating system version and its bitness; and the PostgreSQL version must be identical on the primary and standby PostgreSQL instance.
The steps below are experimental and set up a basic system only.
Further hardening and security setup is needed to have a production system.
Depending on the use of the PostgreSQL cluster a load balancer --like Pgpool-II or HAProxy may be needed to assist with failover and other tasks.
For details see https://www.pgpool.net and https://www.haproxy.org.
To setup a primary and standby PostgreSQL instance, perform the following steps:
Primary server
Install PostgreSQL on the primary server
Enable remote connection for Email Gateway Security instances
- Edit
C:\Program Files\PostgreSQL\14\data\postgresql.conf
- Configure
listen_addresses
to listen on addresses accessible for Email Gateway Security hosts. For details see https://www.postgresql.org/docs/14/runtime-config-connection.html.- Example:
listen_addresses = '*'
- Example:
- Edit
Enable remote authentication for Email Gateway Security instances
- Edit
C:\Program Files\PostgreSQL\14\data\pg_hba.conf
- Add entry to allow authentication from other hosts. For details see https://www.postgresql.org/docs/14/auth-pg-hba-conf.html.
- Example:
host all postgres 172.0.0.0/8 scram-sha-256
- Example:
- Edit
Enable PostgreSQL port (default
5432
) through the firewallConfigure PostgreSQL for replication
In PostgreSQL create a user for replication
- Example:
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'replicator';
- Example:
Configure postgreSQL for streaming replication
Edit
C:\Program Files\PostgreSQL\14\data\postgresql.conf
Set
wal_level = replica
.Set
archive_mode = on
.Set
hot_standby = on
.Set
archive_command
to a reasonable value.- For details see https://www.postgresql.org/docs/14/runtime-config-wal.html.
Set
wal_keep_size
to a reasonable value.Set
max_wal_senders
to a reasonable value.- For details see https://www.postgresql.org/docs/14/runtime-config-replication.html.
Example:
wal_level = replica
max_wal_senders = 10
wal_keep_size = 10000
archive_mode = on
hot_standby = on
archive_command = 'copy "%p" "c:\\temp\\archivedir\\%f"'
Create the directory for the WAL archives
- Example:
mkdir C:\temp\archivedir
- Example:
Enable remote authentication for replicas
- Edit
C:\Program Files\PostgreSQL\14\data\pg_hba.conf
- Add entry to allow authentication from other hosts. Use the PostgreSQL user that was created in step 5.a. For details see https://www.postgresql.org/docs/14/auth-pg-hba-conf.html.
- Example:
host all replicator 172.0.0.0/8 scram-sha-256
- Example:
- Edit
Standby server
Install PostgreSQL on the standby server and stop the PostgreSQL service
Empty the
C:\Program Files\PostgreSQL\14\data
directory- Example:
cd C:\Program Files\PostgreSQL\14\data
del *.*
- Example:
Take a base backup on the primary server and apply it on the standby. Use the PostgreSQL user and password that was created in step Primary server / 5.a.
- Example:
"C:\Program Files\PostgreSQL\14\bin\pg_basebackup.exe" -D "c:\Program Files\PostgreSQL\14\data" -h win11-a -p 5432 -Xs -R -P -U replicator --password
- Example:
Create the
C:\Program Files\PostgreSQL\14\data\standby.signal
file (should be created by the previous step)Start the PostgreSQL service
Verify that replication works for example with adding records on the primary instance that should show up on the standby.
For details see https://www.enterprisedb.com/blog/how-set-streaming-replication-keep-your-postgresql-database-performant-and-date.
Database as a service
PostgreSQL is offered by major SaaS providers. PostgreSQL as a service can be used as the database cluster for Email Gateway Security as long as the prerequisites are fulfilled. For details see Software prerequisites.
For certain PostgreSQL SaaS offerings the required extensions may not be enabled.
As an example the following article details how to allow extensions in Azure PostgreSQL: https://learn.microsoft.com/en-us/azure/postgresql/extensions/how-to-allow-extensions.
Failover
PostgreSQL does not provide the system software required to identify a failure on the primary and notify the standby database server. Many such tools exist and are well integrated with the operating system facilities required for successful failover, such as IP address migration.
It is possible to manually detect the primary server being down and switching to the standby server. After the primary server gets down the manual method requires two high level steps:
- Promote the standby. For details see https://www.postgresql.org/docs/14/warm-standby-failover.html.
- Configure each Email Gateway Security instance to connect (
db_host
,db_port
) and authenticate (db_user_name
) to the promoted server. For details see Registry configuration.
The recommended way is to use automated tools like Pgpool-II and HAProxy to assist the failover.