-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate
82 lines (60 loc) · 2.25 KB
/
generate
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
### Generate necessary files.
###
### Usage:
### $ bash generate -1 user -2 password -3 database
echo "Generating all necessary files..."
export PGUSER="${1}"
export PGPASSWORD="${2}"
export PGDATABASE="${3}"
echo "Generating postgres init transaction..."
INIT_SQL_SCRIPT="DO
\$\$
BEGIN
-- Create the database if it doesn't exist
IF EXISTS (SELECT FROM pg_database WHERE datname = '$PGDATABASE') THEN
RAISE NOTICE 'Database already exists';
ELSE
PERFORM dblink_exec('dbname=' || current_database() -- current db
, 'CREATE DATABASE $PGDATABASE');
END IF;
-- Create the user if it doesn't exist
IF NOT EXISTS (SELECT FROM pg_catalog.pg_user WHERE usename = '$PGUSER') THEN
CREATE USER $PGUSER WITH PASSWORD '$PGPASSWORD';
-- Grant necessary privileges to the user
-- REPLACE WITH YOUR DESIRED PRIVILEGES
GRANT ALL PRIVILEGES ON DATABASE $PGDATABASE TO $PGUSER;
END IF;
END
\$\$;"
cat <<<$INIT_SQL_SCRIPT >compose/postgres/init.sql
echo "Generating postgres env file..."
POSTGRES_ENV_FILE_CONTENT="POSTGRES_USER=$PGUSER
POSTGRES_PASSWORD=$PGPASSWORD
POSTGRES_DB=$PGDATABASE"
cat <<<$POSTGRES_ENV_FILE_CONTENT >compose/.envs/.postgres
echo "Generating pgbouncer env file..."
PGBOUNCER_ENV_FILE_CONTENT="POSTGRESQL_HOST=postgres
POSTGRESQL_PORT_NUMBER=5432
POSTGRESQL_USER=$PGUSER
POSTGRESQL_PASSWORD=$PGPASSWORD
POSTGRESQL_DATABASE=$PGDATABASE"
cat <<<$PGBOUNCER_ENV_FILE_CONTENT >compose/.envs/.pgbouncer
echo "Generating pgbouncer ini file..."
PGBOUNCER_INI_FILE_CONTENT="[databases]
$PGDATABASE = host=postgres port=5432 dbname=$PGDATABASE user=$PGUSER password=$PGPASSWORD
[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432
auth_type = md5
auth_file = /bitnami/pgbouncer/userlist.txt
logfile = /bitnami/pgbouncer/pgbouncer.log
pidfile = /bitnami/pgbouncer/pgbouncer.pid
default_pool_size = 120
max_client_conn = 400"
cat <<<$PGBOUNCER_INI_FILE_CONTENT >compose/pgbouncer/pgbouncer.ini
echo "Generating pgbouncer userlist file..."
printf '%s' $PGPASSWORD$PGUSER | md5sum | awk '{print $1}' >compose/pgbouncer/userlist.txt
md5password=$(cat compose/pgbouncer/userlist.txt)
cat <<<"\"$PGUSER\" \"md5$md5password\"" >compose/pgbouncer/userlist.txt
echo "Done!"