forked from Painted-Fox/docker-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_run.sh
More file actions
39 lines (32 loc) · 1.02 KB
/
first_run.sh
File metadata and controls
39 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
USER=${USER:-super}
PASS=${PASS:-$(pwgen -s -1 16)}
pre_start_action() {
# Echo out info to later obtain by running `docker logs container_name`
echo "POSTGRES_USER=$USER"
echo "POSTGRES_PASS=$PASS"
echo "POSTGRES_DATA_DIR=$DATA_DIR"
# test if DATA_DIR has content
if [[ ! "$(ls -A $DATA_DIR)" ]]; then
echo "Initializing PostgreSQL at $DATA_DIR"
# Copy the data that we generated within the container to the empty DATA_DIR.
cp -R /var/lib/postgresql/9.3/main/* $DATA_DIR
fi
# Ensure postgres owns the DATA_DIR
chown -R postgres $DATA_DIR
# Ensure we have the right permissions set on the DATA_DIR
chmod -R 700 $DATA_DIR
}
post_start_action() {
echo "Creating the superuser: $USER"
until su postgres -c "psql -q <<-EOF
DROP ROLE IF EXISTS $USER;
CREATE ROLE $USER WITH ENCRYPTED PASSWORD '$PASS';
ALTER ROLE $USER WITH SUPERUSER;
ALTER ROLE $USER WITH LOGIN;
EOF"; do
echo 'were unable to create super user'
echo 'retrying in 3 seconds'
sleep 3
done
rm /firstrun
}