forked from OpenDataEnsemble/synkronus-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_sync_db.sh
More file actions
43 lines (35 loc) · 1.29 KB
/
create_sync_db.sh
File metadata and controls
43 lines (35 loc) · 1.29 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
#!/bin/bash
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <username> [--recreate]"
exit 1
fi
USERNAME="$1"
RECREATE=false
if [ "$2" == "--recreate" ]; then
RECREATE=true
fi
DB_USER="synk_$USERNAME"
DB_NAME="synk_$USERNAME"
PASSWORD=$(openssl rand -base64 30 | tr -d /=+ | cut -c1-48)
SUGGESTED_ADMIN_PASSWORD=$(openssl rand -base64 30 | tr -d /=+ | cut -c1-48)
# Check if database exists
DB_EXISTS=$(docker exec postgres psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME';")
if [ "$DB_EXISTS" == "1" ]; then
if [ "$RECREATE" = false ]; then
echo "User and DB already exists - use '--recreate' flag to recreate them"
exit 0
else
# Drop user and database
docker exec postgres psql -U postgres -c "DROP DATABASE IF EXISTS $DB_NAME;"
docker exec postgres psql -U postgres -c "DROP ROLE IF EXISTS $DB_USER;"
echo "User and DB recreated"
fi
fi
# Create role and database
docker exec postgres psql -U postgres -c "CREATE ROLE $DB_USER LOGIN PASSWORD '$PASSWORD';"
docker exec postgres psql -U postgres -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
echo "Database created!"
echo "Connection string:"
echo "postgres://$DB_USER:$PASSWORD@db:5432/$DB_NAME?sslmode=disable"
echo "Suggested synk_admin password: $SUGGESTED_ADMIN_PASSWORD"