Skip to content

Commit dc99a97

Browse files
Start postgres separately while creating the DB
Use pg_ctl with -w start to ensure postgres is started. Allows create_db to run in the foreground on start for CREATE_DB_ON_BOOT. Take inspiration to how docker-library postgres does similar: https://github.com/docker-library/postgres/blob/889f9447cd2dfe21cccfbe9bb7945e3b037e02d8/15/bullseye/docker-entrypoint.sh#L294-L316
1 parent c895e74 commit dc99a97

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

templates/postgres.template.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ run:
3030
rm /root/install_postgres
3131
fi
3232
if [ "$CREATE_DB_ON_BOOT" = "1" ]; then
33-
/usr/local/bin/create_db&
33+
sudo -E -u postgres /usr/lib/postgresql/15/bin/pg_ctl -D /etc/postgresql/15/main -o "-c listen_addresses='' -p 5432" -w start
34+
/usr/local/bin/create_db
35+
sudo -E -u postgres /usr/lib/postgresql/15/bin/pg_ctl -D /etc/postgresql/15/main -m fast -w stop
3436
fi
3537
HOME=/var/lib/postgresql USER=postgres exec thpoff chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/15/bin/postmaster -D /etc/postgresql/15/main
3638
@@ -245,11 +247,6 @@ run:
245247
chmod: +x
246248
contents: |
247249
#!/bin/bash
248-
# wait for postgres to start up...
249-
for i in {1..5}; do
250-
su postgres -c 'pg_isready -q' && break
251-
sleep 1
252-
done
253250
su postgres -c 'createdb $db_name' || true
254251
su postgres -c 'psql $db_name -c "create user $db_user;"' || true
255252
su postgres -c 'psql $db_name -c "grant all privileges on database $db_name to $db_user;"' || true
@@ -289,5 +286,7 @@ run:
289286
tag: db
290287
hook: postgres
291288
cmd:
289+
# wait for postgres to start up...
290+
- sleep 5
292291
- /usr/local/bin/create_db
293292
- "echo postgres installed!"

0 commit comments

Comments
 (0)