File tree Expand file tree Collapse file tree 3 files changed +71
-6
lines changed Expand file tree Collapse file tree 3 files changed +71
-6
lines changed Original file line number Diff line number Diff line change @@ -5,14 +5,18 @@ services:
5
5
networks :
6
6
- devnet
7
7
volumes :
8
- - " psqldata:/var/lib/postgresql/data"
9
- - " ./scripts/postgres_init.sql:/docker-entrypoint-initdb.d/postgres_init.sql"
8
+ - psqldata:/var/lib/postgresql/data
9
+ - ./scripts/postgres_init.sql:/docker-entrypoint-initdb.d/postgres_init.sql:ro
10
+ - ./scripts/postgres_always.sql:/postgres_always.sql:ro
11
+ - ./scripts/postgres_always.sh:/postgres_always.sh:ro
12
+ - ./scripts/postgres_setup.sh:/usr/local/bin/postgres_setup.sh:ro
10
13
restart : unless-stopped
11
14
healthcheck :
12
- test : ["CMD-SHELL", "psql -U fence_user -d fence_db -c 'SELECT 1;'"]
13
- interval : 60s
14
- timeout : 5s
15
- retries : 3
15
+ test : ["CMD-SHELL", "psql -U fence_user -d fence_db -c 'SELECT 1;'"]
16
+ interval : 60s
17
+ timeout : 5s
18
+ retries : 3
19
+ command : postgres_setup.sh
16
20
environment :
17
21
- POSTGRES_PASSWORD=postgres
18
22
#
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+
3
+ create_db_idempotent () {
4
+ # Creating a DB similar to the "IF NOT EXISTS" syntax is a bit challenging in
5
+ # Postgres.
6
+ psql -U postgres -tc " SELECT 1 FROM pg_database WHERE datname = '${1} '" | grep -q 1 || \
7
+ psql -U postgres -c " CREATE DATABASE ${1} "
8
+ }
9
+
10
+ create_user_idempotent () {
11
+ psql -U postgres << EOF
12
+ DO \$\$
13
+ BEGIN
14
+ IF NOT EXISTS(SELECT 1 FROM pg_roles WHERE rolname='${1} ') THEN
15
+ CREATE USER ${1} ;
16
+ END IF;
17
+ END
18
+ \$\$ ;
19
+ EOF
20
+ }
21
+
22
+ create_db_idempotent " metadata"
23
+ create_user_idempotent " metadata_user"
24
+
25
+ psql -U postgres << EOF
26
+ ALTER USER metadata_user WITH PASSWORD 'metadata_pass';
27
+ ALTER USER metadata_user WITH SUPERUSER;
28
+ EOF
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+
3
+ # Thing shim around the normal Docker postgres entrypoint that allows us to run
4
+ # non-application migrations. Things like DB and user creations that would be
5
+ # done by cloud-automation tasks in a normal env.
6
+
7
+ set -e
8
+
9
+ # Initialize the DB, but don't allow outside connections yet.
10
+ docker-entrypoint.sh postgres -c listen_addresses=' 127.0.0.1' &
11
+ # Wait until the server is out of initialization mode and online.
12
+ while ! psql -U postgres -h localhost -c ' SELECT 1;' 2> /dev/null; do sleep 1; done
13
+ # Stop the server.
14
+ gosu postgres pg_ctl stop
15
+
16
+ echo " [postgres] run migrations"
17
+
18
+ # Run migrations/scripts that should run on every start. This is handy for data
19
+ # we want to backfill or otherwise migrate for users.
20
+ gosu postgres bash -c " (
21
+ source /usr/local/bin/docker-entrypoint.sh
22
+ docker_setup_env
23
+ docker_temp_server_start
24
+
25
+ bash /postgres_always.sh
26
+
27
+ docker_temp_server_stop
28
+ )"
29
+
30
+ echo " [postgres] migrations complete"
31
+
32
+ # Start postgres "normally" allowing all network clients to connect.
33
+ docker-entrypoint.sh postgres
You can’t perform that action at this time.
0 commit comments