-
Notifications
You must be signed in to change notification settings - Fork 86
/
prepare-database-postgresql.sh
47 lines (40 loc) · 1.36 KB
/
prepare-database-postgresql.sh
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
#!/bin/bash
# set -ex
# read variables from environment
db_host=${POSTGRES_HOST:-}
db_port=${POSTGRES_PORT:-5432}
db_username=${POSTGRES_USER:-}
db_password=${POSTGRES_PASSWORD:-} # default to empty password
db_name=${CNPMCORE_DATABASE_NAME:-cnpmcore_unittest}
# prepare PostgreSQL param
param=""
if [ -n "$db_host" ]; then
param="$param --host=$db_host"
fi
if [ -n "$db_port" ]; then
param="$param --port=$db_port"
fi
if [ -n "$db_username" ]; then
param="$param --username=$db_username"
fi
if [ -n "$db_password" ]; then
# https://stackoverflow.com/questions/6405127/how-do-i-specify-a-password-to-psql-non-interactively
export PGPASSWORD=$db_password
fi
# reset database
echo "️😈 Reset database $db_name in local"
dropdb $param $db_name || echo "ignore database not exists"
# http://www.postgres.cn/docs/15/app-createdb.html
createdb $param --echo --encoding=UTF8 $db_name
# find all sql files and sort
sql_files=$(ls sql/postgresql/*.sql | sort)
echo "🤖 Running the following SQL files:"
# execute sql files
for file in $sql_files; do
echo "🔖 Running $file..."
# psql $param --dbname=$db_name --file=$file --echo-all
psql $param --dbname=$db_name --file=$file --quiet
done
echo "🎉 prepare database $db_name done"
# psql $param --dbname=$db_name -c "SELECT * FROM pg_catalog.pg_tables where schemaname = 'public';"
psql $param --dbname=$db_name -c "\dt"