-
Notifications
You must be signed in to change notification settings - Fork 3
/
data2pg_init_admin_db.sh
executable file
·221 lines (183 loc) · 6.4 KB
/
data2pg_init_admin_db.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/bash
# data2pg_init_admin_db.sh
# This shell script initializes the data2pg administration database
echo "=================================================="
echo " Create the Data2Pg administration database "
echo "=================================================="
# Environment variables
## My values
export DATA2PG_ROLE=data2pg_adm
export DATA2PG_PWD=secret
export DATA2PG_ADMIN_SCHEMA=data2pg0.7
## Default values
PGHOST_DEFAULT_VALUE=localhost
PGPORT_DEFAULT_VALUE=5432
PGUSER_DEFAULT_VALUE=postgres
PGDATABASE_DEFAULT_VALUE=data2pg
DATA2PG_ADMIN_SCHEMA_DEFAULT_VALUE=data2pg
DATA2PG_ROLE_DEFAULT_VALUE=data2pg
DATA2PG_PWD_DEFAULT_VALUE=gp2atad
TARGET_DB_FILE_DEFAULT_VALUE=target_database.dat
if [ -z ${PGHOST+x} ];
then
echo "Setting environment variable PGHOST to its default value: ${PGHOST_DEFAULT_VALUE}"
export PGHOST=${PGHOST_DEFAULT_VALUE}
else
echo "The environment variable PGHOST is already defined: ${PGHOST}"
fi
if [ -z ${PGPORT+x} ];
then
echo "Setting environment variable PGPORT to its default value: ${PGPORT_DEFAULT_VALUE}"
export PGPORT=${PGPORT_DEFAULT_VALUE}
else
echo "The environment variable PGPORT is already defined: ${PGPORT}"
fi
if [ -z ${PGUSER+x} ];
then
echo "Setting environment variable PGUSER to its default value: ${PGUSER_DEFAULT_VALUE}"
export PGUSER=${PGUSER_DEFAULT_VALUE}
else
echo "The environment variable PGUSER is already defined: ${PGUSER}"
fi
if [ -z ${PGDATABASE+x} ];
then
echo "Setting environment variable PGDATABASE to its default value: ${PGDATABASE_DEFAULT_VALUE}"
export PGDATABASE=${PGDATABASE_DEFAULT_VALUE}
else
echo "The environment variable PGDATABASE is already defined: ${PGDATABASE}"
fi
if [ -z ${DATA2PG_ADMIN_SCHEMA+x} ];
then
echo "Setting environment variable DATA2PG_ADMIN_SCHEMA to its default value: ${DATA2PG_ADMIN_SCHEMA_DEFAULT_VALUE}"
export DATA2PG_ADMIN_SCHEMA=${DATA2PG_ADMIN_SCHEMA_DEFAULT_VALUE}
else
echo "the environment variable DATA2PG_ADMIN_SCHEMA is already defined: ${DATA2PG_ADMIN_SCHEMA}"
fi
if [ -z ${DATA2PG_ROLE+x} ];
then
echo "Setting environment variable DATA2PG_ROLE to its default value: ${DATA2PG_ROLE_DEFAULT_VALUE}"
export DATA2PG_ROLE=${DATA2PG_ROLE_DEFAULT_VALUE}
else
echo "The environment variable DATA2PG_ROLE is already defined: ${DATA2PG_ROLE}"
fi
if [ -z ${DATA2PG_PWD+x} ];
then
echo "Setting environment variable DATA2PG_PWD to its default value: ${DATA2PG_PWD_DEFAULT_VALUE}"
export DATA2PG_PWD=${DATA2PG_PWD_DEFAULT_VALUE}
else
echo "The environment variable DATA2PG_PWD is already defined"
fi
if [ -z ${TARGET_DB_FILE+x} ];
then
echo "Setting environment variable TARGET_DB_FILE to its default value: ${TARGET_DB_FILE_DEFAULT_VALUE}"
export TARGET_DB_FILE=${TARGET_DB_FILE_DEFAULT_VALUE}
else
echo "The environment variable TARGET_DB_FILE is already defined: ${TARGET_DB_FILE}"
fi
echo "Create the owner role"
echo "---------------------"
psql postgres -v pgdatabase=$PGDATABASE<<EOF
\set ON_ERROR_STOP ON
-- Create the owner role if needed and the database.
CREATE OR REPLACE FUNCTION public.create_database(p_dbname TEXT) RETURNS void LANGUAGE plpgsql AS
\$create_database\$
BEGIN
-- Create the data2pg role
IF NOT EXISTS
(SELECT 0
FROM pg_catalog.pg_roles
WHERE rolname = 'data2pg'
) THEN
CREATE ROLE data2pg NOLOGIN;
COMMENT ON ROLE data2pg IS
'Owner of the data2pg and/or data2pg_admin extensions.';
END IF;
END;
\$create_database\$;
SELECT public.create_database(:'pgdatabase');
DROP FUNCTION public.create_database;
EOF
if [ $? -ne 0 ]; then
echo " => Problem encountered"
exit 1
else
echo " => data2pg role successfuly created"
fi
echo "Create the database (if it does not exist)"
echo "------------------------------------------"
psql template1 -tc "SELECT 1 FROM pg_database WHERE datname = '${PGDATABASE}'" | grep -q 1 || psql template1 -c "CREATE DATABASE ${PGDATABASE} OWNER data2pg"
if [ $? -ne 0 ]; then
echo " => Problem encountered"
exit 1
else
echo " => Administration database created"
fi
echo "Create the data2pg administration extension"
echo "-------------------------------------------"
psql -v admin_schema=$DATA2PG_ADMIN_SCHEMA<<EOF
\set ON_ERROR_STOP ON
CREATE OR REPLACE FUNCTION public.create_extension(p_schema TEXT) RETURNS void LANGUAGE plpgsql AS
\$create_extension\$
BEGIN
-- Drop the extension and schema, if any.
DROP EXTENSION IF EXISTS data2pg_admin;
EXECUTE format('DROP SCHEMA IF EXISTS %I CASCADE', p_schema);
-- Create the schema and extension.
EXECUTE format('CREATE SCHEMA %I', p_schema);
EXECUTE format('CREATE EXTENSION data2pg_admin SCHEMA %I', p_schema);
--
RETURN;
END;
\$create_extension\$;
SELECT public.create_extension(:'admin_schema');
DROP FUNCTION public.create_extension;
EOF
if [ $? -ne 0 ]; then
echo " => Problem encountered"
exit 1
else
echo " => data2pg_admin extension successfuly created"
fi
echo "Create the log on role to be used, if needed"
echo "--------------------------------------------"
psql -v data2pg_role=${DATA2PG_ROLE} -v data2pg_pwd=${DATA2PG_PWD}<<EOF
\set ON_ERROR_STOP ON
CREATE OR REPLACE FUNCTION public.create_role(p_role TEXT, p_pwd TEXT) RETURNS void LANGUAGE plpgsql AS
\$create_role\$
BEGIN
-- If the connection role is data2pg, just set/reset its password (It has been created by the already executed "CREATE EXTENSION data2pg" statement).
IF p_role = 'data2pg' THEN
EXECUTE format('ALTER ROLE data2pg LOGIN PASSWORD %L', p_pwd);
ELSE
-- Otherwise create the requested role if it does not already exist and grant it data2pg.
PERFORM 0 FROM pg_catalog.pg_roles WHERE rolname = p_role;
IF NOT FOUND THEN
EXECUTE format('CREATE ROLE %I LOGIN PASSWORD %L', p_role, p_pwd);
END IF;
EXECUTE format('GRANT data2pg TO %I', p_role);
END IF;
--
RETURN;
END;
\$create_role\$;
SELECT public.create_role(:'data2pg_role', :'data2pg_pwd');
DROP FUNCTION public.create_role;
EOF
if [ $? -ne 0 ]; then
echo " => Problem encountered"
exit 1
else
echo " => The connection role is created"
fi
echo "Load the target databases list"
echo "------------------------------"
psql -U ${DATA2PG_ROLE} -c "\copy \"${DATA2PG_ADMIN_SCHEMA}\".target_database FROM $TARGET_DB_FILE CSV HEADER"
if [ $? -ne 0 ]; then
echo " => Problem encountered"
exit 1
else
echo " => target databases successfuly loaded."
fi
echo ""
echo "The data2pg administration database is ready"
echo ""