Skip to content

Commit

Permalink
db_init option (#2)
Browse files Browse the repository at this point in the history
* add DB_INIT option
* update readme
* general improvment
  • Loading branch information
barcus authored Mar 26, 2022
1 parent fbcc562 commit fc8d478
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ Either way, the command is run with postgres system user.
After successful upgrade, new database is available in `<pg_new_data>`
folder. In any case source database is not modified.

Docker volumes required:
PostgreSQL version, database encoding and locale are discovered from
source database.

* `/data/pg-old` (should contain source db)
* `/data/pg-new` (will contain target db)
:warning: Be sure to set PGUSER var with an existing superuser in source DB

See example below.

Variables:
Docker volumes required:

* PGUSER=postgres (should exist in source database)
* `/data/pg-old` (should contain source db)
* `/data/pg-new` (will contain target db)

Optional:
Variables:

* PG_NEW=14 (default v14)
* PG_OLD (discovered from source PG_VERSION file)
* ENCODING (discovered from source $PGUSER database)
* LOCALE (discovered from source $PGUSER database)
* PGUSER (default postgres)
* PG_NEW (default 14)
* DB_INIT (default true)

### Example

Expand Down
21 changes: 10 additions & 11 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#!/usr/bin/env bash
#set -x

if [ -z "${PGUSER}" ] ; then
echo 'ERROR: var PGUSER required'
exit 1
fi

[ -z "${PG_NEW}" ] && PG_NEW=14

: "${PGUSER:=postgres}"
: "${PG_NEW:=14}"
: "${DB_INIT:=true}"
PG_OLD=$(cat /pg_old/data/PG_VERSION)

re='^[0-9]+([.][0-9]+)?$'
if ! [[ ${PG_OLD} =~ $re ]] ; then
echo "ERROR: /pg_old/data/PG_VERSION file not found" >&2; exit 1
echo "ERROR: version not found in file /pg_old/data/PG_VERSION" >&2; exit 1
fi

export PGBINOLD=/usr/lib/postgresql/${PG_OLD}/bin
Expand Down Expand Up @@ -61,9 +58,11 @@ export LOCALE=$(echo ${LOCALE}| xargs)
eval "${PGBINOLD}/pg_ctl -D ${PGDATAOLD} -l logfile stop"

# Init DB PG_NEW
[ -z "${ENCODING}" ] && ENCODING="SQL_ASCII"
[ -z "${LOCALE}" ] && LOCALE="en_US.utf8"
eval "${PGBINNEW}/initdb --username=${PGUSER} --pgdata=${PGDATANEW} --encoding=${ENCODING} --lc-collate=${LOCALE} --lc-ctype=${LOCALE}"
if [ "${DB_INIT}" == 'true' ]; then
ENCODING="${ENCODING:=SQL_ASCII}"
LOCALE="${LOCALE:=en_US.utf8}"
eval "${PGBINNEW}/initdb --username=${PGUSER} --pgdata=${PGDATANEW} --encoding=${ENCODING} --lc-collate=${LOCALE} --lc-ctype=${LOCALE}"
fi

# run pg_upgrade or launch CMD
if [ "$1" = 'pg_upgrade' ] ;then
Expand Down

0 comments on commit fc8d478

Please sign in to comment.