File tree Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 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
24set  -e
35
46#  Script should only run on the primary node
Original file line number Diff line number Diff line change 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
24set  -e
35
46#  Script should only run on the primary node
Original file line number Diff line number Diff line change 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 
Original file line number Diff line number Diff line change 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} " " ${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 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments