Skip to content

Commit 873acd0

Browse files
committed
Adding script to start Postgres 12 via Docker, and utility function
1 parent 60a97bf commit 873acd0

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

common.sh

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ function kill-existing() {
99
docker kill "$image_name" 2>/dev/null || echo "Nothing to kill"
1010
docker rm "$image_name" 2>/dev/null || echo "Nothing to remove"
1111
}
12+
13+
function create-network-if-needed() {
14+
NETWORK_NAME="$1"
15+
if [ -z $(docker network ls --filter name=^${NETWORK_NAME}$ --format="{{ .Name }}") ] ; then
16+
docker network create ${NETWORK_NAME} ;
17+
fi
18+
}

run-postgresql-12.sh

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
SOURCE_DIR=`dirname "${BASH_SOURCE[0]}"`
4+
source "$SOURCE_DIR/common.sh"
5+
6+
CONTAINER_NAME=pgsql-12-metabase
7+
DB_NAME=metabase
8+
DB_USER=metabase
9+
DB_PASSWORD=Password1234
10+
HOST_PORT=${PSGQL_PORT:-5432}
11+
DATA_DIR=${PGSQL_DATA_DIR:-$HOME/metabase-pgsql-data}
12+
DOCKER_NETWORK=psql-metabase-network
13+
14+
kill-existing $CONTAINER_NAME
15+
create-network-if-needed $DOCKER_NETWORK
16+
17+
docker run \
18+
--rm \
19+
-d \
20+
-p $HOST_PORT:5432 \
21+
--network $DOCKER_NETWORK \
22+
-e POSTGRES_USER=$DB_USER \
23+
-e POSTGRES_DB=$DB_NAME \
24+
-e POSTGRES_PASSWORD=$DB_PASSWORD \
25+
-e PGDATA=/var/lib/postgresql/data \
26+
-v $DATA_DIR:/var/lib/postgresql/data:Z \
27+
--name $CONTAINER_NAME \
28+
postgres:12
29+
30+
cat << EOF
31+
Started PostgreSQL 12 port $HOST_PORT via Docker (container name: $CONTAINER_NAME). Data will be persisted in $DATA_DIR on the host machine (delete it to reset).
32+
33+
JDBC URL: jdbc:postgres://localhost:$HOST_PORT/$DB_NAME?user=$DB_USER
34+
35+
Environment variables for Metabase (to use as app DB):
36+
MB_DB_TYPE=postgres MB_DB_DBNAME=$DB_NAME MB_DB_HOST=localhost MB_DB_PASS=$DB_PASSWORD MB_DB_PORT=$HOST_PORT MB_DB_USER=$DB_USER MB_POSTGRES_TEST_USER=$DB_USER
37+
38+
To open a SQL client session:
39+
docker run -it --rm --network $DOCKER_NETWORK postgres:12 psql -h $CONTAINER_NAME -U $DB_USER
40+
And enter the DB user password for $DB_USER: $DB_PASSWORD
41+
EOF

0 commit comments

Comments
 (0)