Skip to content

Commit 0ec4a6f

Browse files
authored
Merge pull request #63 from meyeringh/postgres-initialize-db
Add PostgreSQL automatic database initialization support
2 parents 0e4a3a9 + 7d14508 commit 0ec4a6f

File tree

6 files changed

+2409
-11
lines changed

6 files changed

+2409
-11
lines changed

06_initialize_db.sh

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#!/bin/bash
22
INIT_DB=${DB_PASSWORD:-FALSE}
3+
IDB_TYPE=${DB_TYPE:-mysql}
34
IDB_HOST=${DB_HOST:-localhost}
45
IDB_PORT=${DB_PORT:-3306}
56
IDB_USER=${DB_USER:-root}
7+
8+
# Change default port and database user if using postgres
9+
if [ "$IDB_TYPE" = "postgres" ]; then
10+
IDB_PORT=${DB_PORT:-5432}
11+
IDB_USER=${DB_USER:-postgres}
12+
fi
13+
614
IDB_PASSWORD=${DB_PASSWORD:-password}
715
IDB_DB_NAME=${DB_NAME:-webtrees}
816
IDB_DB_PREFIX="wt_"
@@ -20,6 +28,7 @@ then
2028
else
2129
echo "Creating the initial database settings in configuration file $CONFIG_FILE and creating database."
2230
cp /config.ini.php "$CONFIG_FILE"
31+
sed -i 's/<DB_TYPE>/'"$IDB_TYPE"'/g' "$CONFIG_FILE"
2332
sed -i 's/<DB_HOST>/'"$IDB_HOST"'/g' "$CONFIG_FILE"
2433
sed -i 's/<DB_PORT>/'"$IDB_PORT"'/g' "$CONFIG_FILE"
2534
sed -i 's/<DB_USER>/'"$IDB_USER"'/g' "$CONFIG_FILE"
@@ -29,24 +38,44 @@ then
2938
chown www-data:docker-data "$CONFIG_FILE"
3039
chmod 660 "$CONFIG_FILE"
3140
#Create database structure and add admin user
32-
cp /webtrees.sql /mod_webtrees.sql
41+
if [ "$IDB_TYPE" = "postgres" ]; then
42+
SQL_FILE="/webtrees-postgres.sql"
43+
else
44+
SQL_FILE="/webtrees.sql"
45+
fi
46+
47+
cp "$SQL_FILE" /mod_webtrees.sql
3348
sed -i 's/<DB_NAME>/'"$IDB_DB_NAME"'/g' /mod_webtrees.sql
3449
sed -i 's/<WT_ADMIN_NAME>/'"$IDB_WT_ADMIN"'/g' /mod_webtrees.sql
50+
3551
#Encode password and escape for sed
3652
RANDOM22=$(php -r "echo substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22);")
3753
WTSALT=$(php -r "echo '\$2y\$10\$'.str_replace('+','.','$RANDOM22');")
3854
WTCRYPT=$(php -r "echo crypt('$IDB_WT_ADMINPW', '$WTSALT');")
3955
sed -i 's/<WT_ADMIN_PW>/'"$(echo $WTCRYPT | sed -e 's/[]\/$*.^[]/\\&/g')"'/g' /mod_webtrees.sql
4056
sed -i 's/<WT_ADMIN_MAIL>/'"$IDB_WT_ADMINMAIL"'/g' /mod_webtrees.sql
41-
#Write to mysql database
42-
until mysqladmin ping -h "$IDB_HOST" --silent; do
43-
echo "Waiting for database to be ready ..."
44-
sleep 1
45-
done
46-
echo "Database ready. Writing database."
47-
mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" < /mod_webtrees.sql
48-
#Alternative to set Webtrees admin user:
49-
#echo "UPDATE wt_user SET user_name='$IDB_WT_ADMIN', email='$IDB_WT_ADMINMAIL', real_name='Admin', password='$WTCRYPT' WHERE user_id=1" | mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" "$IDB_DB_NAME"
57+
58+
# Wait for database and write schema based on type
59+
if [ "$IDB_TYPE" = "postgres" ]; then
60+
echo "Using PostgreSQL database"
61+
until pg_isready -h "$IDB_HOST" -p "$IDB_PORT" -U "$IDB_USER" > /dev/null 2>&1; do
62+
echo "Waiting for PostgreSQL database to be ready ..."
63+
sleep 1
64+
done
65+
echo "PostgreSQL database ready. Writing database."
66+
export PGPASSWORD="$IDB_PASSWORD"
67+
psql -h "$IDB_HOST" -p "$IDB_PORT" -U "$IDB_USER" -d postgres -f /mod_webtrees.sql
68+
unset PGPASSWORD
69+
else
70+
echo "Using MySQL/MariaDB database"
71+
until mysqladmin ping -h "$IDB_HOST" --silent; do
72+
echo "Waiting for MySQL/MariaDB database to be ready ..."
73+
sleep 1
74+
done
75+
echo "MySQL/MariaDB database ready. Writing database."
76+
mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" < /mod_webtrees.sql
77+
fi
78+
5079
unset RANDOM22
5180
unset WTSALT
5281
unset WTCRYPT

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ RUN apt-get update -qq && apt-get upgrade -qy && apt-get install -qy \
2020
unzip \
2121
sed \
2222
mysql-client \
23+
postgresql-client \
2324
php \
2425
libapache2-mod-php \
2526
php-mysql \
@@ -87,6 +88,7 @@ ADD Auth.php /Auth.php
8788
ADD config.ini.php /config.ini.php
8889
ADD config_blank.ini.php /config_blank.ini.php
8990
ADD webtrees.sql /webtrees.sql
91+
ADD webtrees-postgres.sql /webtrees-postgres.sql
9092
COPY .htaccess /var/www/html/.htaccess
9193
RUN chown -R www-data:www-data /var/www/html \
9294
&& chmod -R 770 /var/www/html

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ It is also possible to active pretty urls by using environment variables. You co
7474

7575
## Automatic initialization of database
7676

77-
For MySQL or MariaDB it is possible to use an automatical initalization of the database within the application. User the parameters to DB_* and WT_* to set the corresponding values. To possible error of the initialization start the container in interactve mode using -it.
77+
For MySQL, MariaDB, or PostgreSQL it is possible to use an automatical initalization of the database within the application. Use the parameters DB_TYPE, DB_* and WT_* to set the corresponding values.
78+
To debug any errors in the initialization process: Start the container in interactve mode using -it.
7879

7980
## Support for PostgreSQL or SQLServer
8081

@@ -90,6 +91,7 @@ This image contains now the necessary libraries to optionally also select Postgr
9091
* `-e ENABLE_REMOTE_USER` - if set to TRUE use REMOTE_USER for authentication
9192
* `-e HEADER_AUTH_VAR` - Sets the name of header variable used for authentication. Default is REMOTE_USER.
9293
* `-e DISABLE_SSL` - if set to TRUE the image only provides an http entpoint. You should also set the port, because default port 443 is not modifed by this setting.
94+
* `-e DB_TYPE` - can only be used at first instantiation to set the database type (mysql, postgres, pgsql, postgresql). Password (DB_PASSWORD) must also be set, otherwise no initial setup is performed. If not present the default will be mysql.
9395
* `-e DB_USER` - can only be used at first instantiation to set the db username. Password (DB_PASSWORD) must also be set (see below), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be root.
9496
* `-e DB_PASSWORD` - can only be used at first instantiation to set the db setting during instantiation and perform initial application setup.
9597
* `-e DB_HOST` - can only be used at first instantiation to set the db hostname (or ip). Password (DB_PASSWORD) must also be set (see above), otherwise no initial setup is performed. If not present (only DB_PASSWORD is present) the default will be localhost.

config.ini.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; <?php exit; ?> DO NOT DELETE THIS LINE
2+
dbtype="<DB_TYPE>"
23
dbhost="<DB_HOST>"
34
dbport="<DB_PORT>"
45
dbuser="<DB_USER>"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
services:
2+
postgres:
3+
image: postgres:18
4+
container_name: webtrees-postgres
5+
environment:
6+
POSTGRES_DB: webtrees
7+
POSTGRES_USER: webtrees
8+
POSTGRES_PASSWORD: webtrees1234
9+
ports:
10+
- "5432:5432"
11+
healthcheck:
12+
test: ["CMD-SHELL", "pg_isready -U webtrees"]
13+
interval: 5s
14+
timeout: 5s
15+
retries: 5
16+
17+
webtrees:
18+
build:
19+
context: .
20+
dockerfile: Dockerfile
21+
container_name: webtrees-app
22+
ports:
23+
- "8080:80"
24+
environment:
25+
- DB_HOST=postgres
26+
- DB_USER=webtrees
27+
- DB_PASSWORD=webtrees1234
28+
- DB_NAME=webtrees
29+
- DB_TYPE=postgres
30+
- WT_ADMIN=admin
31+
- WT_ADMINPW=admin12345
32+
- WT_ADMINMAIL=admin@webtrees.local
33+
- DISABLE_SSL=TRUE
34+
- PORT=80
35+
depends_on:
36+
postgres:
37+
condition: service_healthy

0 commit comments

Comments
 (0)