Skip to content

Commit 2a31cae

Browse files
committed
Updated
1 parent 034aa10 commit 2a31cae

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

scripts/10_primary.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
2+
# Script runs on a primary instance to set up replication slots
3+
# The primary instance should be started before the replicas
24
set -e
35

46
# Script should only run on the primary node

scripts/20_replica.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
2+
# Script runs on a replica node to backup from the primary and set up replication
3+
# The primary instance should be running before starting the replica
24
set -e
35

46
# Script should only run on the primary node

scripts/30_ssl.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
# Script runs on any instance to set up SSL
3+
# https://www.postgresql.org/docs/current/ssl-tcp.html#SSL-OPENSSL-CONFIG
4+
set -e
5+
6+
# Set configuration for replication
7+
CONF="${PGDATA}/postgresql.conf"
8+
9+
if [ ! -z "${POSTGRES_SSL_CERT}" ]; then
10+
if [ ! -f "${POSTGRES_SSL_CERT}" ]; then
11+
echo "POSTGRES_SSL_CERT file not found."
12+
exit 1
13+
fi
14+
sed -i -e"s/^#ssl_cert_file.*$/ssl_cert_file=${POSTGRES_SSL_CERT}/" ${CONF}
15+
fi
16+
17+
if [ ! -z "${POSTGRES_SSL_KEY}" ]; then
18+
if [ ! -f "${POSTGRES_SSL_KEY}" ]; then
19+
echo "POSTGRES_SSL_KEY file not found."
20+
exit 1
21+
fi
22+
sed -i -e"s/^#ssl_key_file.*$/ssl_key_file=${POSTGRES_SSL_KEY}/" ${CONF}
23+
fi
24+
25+
if [ ! -z "${POSTGRES_SSL_CA}" ]; then
26+
if [ ! -f "${POSTGRES_SSL_CA}" ]; then
27+
echo "POSTGRES_SSL_CA file not found."
28+
exit 1
29+
fi
30+
sed -i -e"s/^#ssl_ca_file.*$/ssl_ca_file=${POSTGRES_SSL_CA}/" ${CONF}
31+
fi

scripts/40_databases.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
# Script runs on a primary instance to create databases and roles
3+
set -e
4+
5+
# Script should only run on the primary node
6+
if [ ! -z "${POSTGRES_REPLICATION_PRIMARY}" ]; then
7+
echo "Skipping database initialisation on a replica."
8+
exit 0
9+
fi
10+
11+
# Create the databases
12+
IFS=',' read -r -a array <<< "${POSTGRES_DATABASES}"
13+
for NAME in "${array[@]}"; do
14+
# if there is an environment variable called POSTGRES_PASSWORD_<NAME>, then use that password
15+
if [ ! -z "${!POSTGRES_PASSWORD_${NAME}}" ]; then
16+
ALTER_ROLE="ALTER ROLE ${NAME} WITH PASSWORD ${!POSTGRES_PASSWORD_${NAME}}"
17+
else
18+
ALTER_ROLE="ALTER ROLE ${NAME} WITH NOLOGIN"
19+
fi
20+
# Execute SQL to create the database and role
21+
psql -v ON_ERROR_STOP=1 --username "${POSTGRES_USER}" --dbname "${POSTGRES_DB}" <<-EOSQL
22+
DO $$ BEGIN
23+
-- Create database
24+
CREATE EXTENSION IF NOT EXISTS dblink;
25+
IF NOT EXISTS (
26+
SELECT 1 FROM pg_database WHERE datname = '${NAME}'
27+
) THEN
28+
PERFORM dblink_exec('dbname=${POSTGRES_DB}', 'CREATE DATABASE ${NAME}');
29+
END IF;
30+
31+
-- Create role
32+
CREATE ROLE ${NAME};
33+
${ALTER_ROLE};
34+
ALTER DATABASE ${NAME} OWNER TO ${NAME};
35+
EXCEPTION WHEN duplicate_object THEN RAISE NOTICE '%, skipping', SQLERRM USING ERRCODE = SQLSTATE;
36+
END $$;
37+
EOSQL
38+
done

0 commit comments

Comments
 (0)