-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathfirst_run.sh
More file actions
59 lines (51 loc) · 1.62 KB
/
first_run.sh
File metadata and controls
59 lines (51 loc) · 1.62 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
USER=${DB_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"
if [ ! -z $DB ];then echo "POSTGRES_DB=$DB";fi
# 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 -aR /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"
setuser postgres psql -q <<-EOF
DROP ROLE IF EXISTS $USER;
CREATE ROLE $USER WITH ENCRYPTED PASSWORD '$PASS';
ALTER USER $USER WITH ENCRYPTED PASSWORD '$PASS';
ALTER ROLE $USER WITH SUPERUSER;
ALTER ROLE $USER WITH LOGIN;
EOF
# create database if requested
if [ ! -z "$DB" ]; then
for db in $DB; do
echo "Creating database: $db"
setuser postgres psql -q <<-EOF
CREATE DATABASE $db WITH OWNER=$USER ENCODING='UTF8';
GRANT ALL ON DATABASE $db TO $USER
EOF
done
fi
if [[ ! -z "$EXTENSIONS" && ! -z "$DB" ]]; then
for extension in $EXTENSIONS; do
for db in $DB; do
echo "Installing extension for $db: $extension"
# enable the extension for the user's database
setuser postgres psql $db <<-EOF
CREATE EXTENSION "$extension";
EOF
done
done
fi
rm /firstrun
}