Skip to content

Commit

Permalink
connect to db using postgres user
Browse files Browse the repository at this point in the history
  • Loading branch information
roflcoopter committed Apr 7, 2024
1 parent e7b6337 commit cd4111f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ ENV \
DEBIAN_FRONTEND=noninteractive \
S6_KEEP_ENV=1 \
S6_SERVICES_GRACETIME=30000 \
S6_KILL_GRACETIME=1000 \
S6_KILL_GRACETIME=30000 \
S6_KILL_FINISH_MAXTIME=30000 \
PATH=$PATH:/home/abc/bin \
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib \
PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.8/site-packages \
Expand Down
23 changes: 23 additions & 0 deletions rootfs/etc/cont-finish.d/10-postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/with-contenv bash
#
source /helpers/logger.sh

log_info "Wait for Viseron to stop..."
PID=$(pgrep -f ^viseron)
while ps -p $PID > /dev/null; do
sleep 1
done
log_info "Viseron has stopped!"

log_info "Stopping PostgreSQL..."
s6-setuidgid postgres /usr/lib/postgresql/12/bin/pg_ctl -D /config/postgresql -l /config/postgresql/logfile stop

# Wait until PostgreSQL has stopped
log_info "Waiting for PostgreSQL Server to stop..."
while s6-setuidgid abc pg_isready -d viseron; do
sleep 1
done
log_info "PostgreSQL Server has stopped!"

# Restore permissions
chown -R abc:abc /config/postgresql
15 changes: 7 additions & 8 deletions rootfs/etc/cont-init.d/80-postgres
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ source /helpers/logger.sh
log_info "***************** Preparing PostgreSQL *******************"
mkdir -p /config/postgresql
mkdir -p /var/run/postgresql
chown abc:abc /var/run/postgresql
chown -R abc:abc /config/postgresql
chown postgres:abc /var/run/postgresql
chown -R postgres:abc /config/postgresql
chmod 0700 /config/postgresql

if [ -e /config/postgresql/postgresql.conf ]; then
log_info "Database has already been initialized."
else
log_info "Database has not been initialized. Initializing..."
s6-setuidgid abc /usr/lib/postgresql/12/bin/initdb
s6-setuidgid postgres /usr/lib/postgresql/12/bin/initdb
log_info "Starting PostgreSQL..."
s6-setuidgid abc /usr/lib/postgresql/12/bin/pg_ctl -D /config/postgresql -l /config/postgresql/logfile start
if s6-setuidgid abc psql -q -d viseron -tc "SELECT 1 FROM pg_database WHERE datname = 'viseron';" ; then
s6-setuidgid postgres /usr/lib/postgresql/12/bin/pg_ctl -D /config/postgresql -l /config/postgresql/logfile start
if s6-setuidgid postgres psql -q -d viseron -tc "SELECT 1 FROM pg_database WHERE datname = 'viseron';" ; then
log_info "Database has already been created."
else
log_info "Database has not been created. Creating..."
s6-setuidgid abc createuser viseron
s6-setuidgid abc createdb -U abc -O viseron viseron
s6-setuidgid postgres createdb -U postgres -O postgres viseron
fi
log_info "Stopping PostgreSQL..."
s6-setuidgid abc /usr/lib/postgresql/12/bin/pg_ctl -D /config/postgresql -l /config/postgresql/logfile stop
s6-setuidgid postgres /usr/lib/postgresql/12/bin/pg_ctl -D /config/postgresql -l /config/postgresql/logfile stop
fi

log_info "*********************** Done *****************************"
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/postgres/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash

echo "Starting PostgreSQL Server..."
s6-setuidgid abc /usr/lib/postgresql/12/bin/postgres -D /config/postgresql
s6-setuidgid postgres /usr/lib/postgresql/12/bin/postgres -D /config/postgresql
2 changes: 1 addition & 1 deletion rootfs/etc/services.d/viseron/run
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pkill -f ffmpeg_
pkill -f gstreamer_

# Wait until PostgreSQL is ready
until s6-setuidgid abc pg_isready -d viseron; do
until s6-setuidgid postgres pg_isready -d viseron; do
log_info "Waiting for PostgreSQL Server to start..."
sleep 1
done
Expand Down
2 changes: 1 addition & 1 deletion viseron/components/storage/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

COMPONENT = "storage"

DATABASE_URL = "postgresql://localhost/viseron"
DATABASE_URL = "postgresql://postgres@localhost/viseron"

MOVE_FILES_THROTTLE_SECONDS = 10

Expand Down

0 comments on commit cd4111f

Please sign in to comment.