Mercurial > lada > lada-server
annotate db_schema/setup-db.sh @ 1303:a0da035cc831
Do not allow access to unknow classes.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Tue, 28 Feb 2017 14:03:51 +0100 |
parents | ba47994c1665 |
children | b5ab25b7ac2c |
rev | line source |
---|---|
933 | 1 #!/bin/sh -e |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
2 # SYNOPSIS |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
3 # ./setup-db.sh [-cn] [ROLE_NAME] [ROLE_PW] [DB_NAME] |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
4 # -c clean - drop an existing database |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
5 # -n no data - do not import example data |
934 | 6 # ROLE_NAME name of db user (default = lada) |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
7 # ROLE_PW login password (default = ROLE_NAME) |
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
8 # DB_NAME name of the databaes (default = ROLE_NAME) |
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
9 # |
934 | 10 # There will be used a remote database server if there exists the |
11 # enviroment variable DB_SRV and optional DB_PORT | |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
12 |
1200 | 13 DIR=$(readlink -f $(dirname $0)) |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
14 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
15 while getopts "cn" opt; do |
932
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
16 case "$opt" in |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
17 c) |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
18 DROP_DB="true" |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
19 ;; |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
20 n) |
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
21 NO_DATA="true" |
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
22 ;; |
932
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
23 esac |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
24 done |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
25 |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
26 shift $((OPTIND-1)) |
930
d3d9cd5e18f8
add initialze sequences in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
928
diff
changeset
|
27 |
854
e797391714a0
Allow setting variables via commandline.
Tom Gottfried <tom@intevation.de>
parents:
764
diff
changeset
|
28 ROLE_NAME=${1:-lada} |
934 | 29 echo "ROLE_NAME = $ROLE_NAME" |
854
e797391714a0
Allow setting variables via commandline.
Tom Gottfried <tom@intevation.de>
parents:
764
diff
changeset
|
30 ROLE_PW=${2:-$ROLE_NAME} |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
31 echo "ROLE_PW = $ROLE_PW" |
854
e797391714a0
Allow setting variables via commandline.
Tom Gottfried <tom@intevation.de>
parents:
764
diff
changeset
|
32 DB_NAME=${3:-$ROLE_NAME} |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
33 echo "DB_NAME = $DB_NAME" |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
34 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
35 # if variable DB_SRV and otional DB_PORT is set a remote database connection will be used |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
36 if [ -n "$DB_SRV" ] ; then DB_CONNECT_STRING="-h $DB_SRV" ; fi |
934 | 37 if [ -n "$DB_SRV" -a -n "$DB_PORT" ] ; then |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
38 DB_CONNECT_STRING="$DB_CONNECT_STRING -p $DB_PORT" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
39 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
40 DB_CONNECT_STRING="$DB_CONNECT_STRING -U postgres" |
928
6ab17d8b5558
erase mpr_id values in lada_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
925
diff
changeset
|
41 echo "DB_CONNECT_STRING = $DB_CONNECT_STRING" |
866
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
42 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
43 if [ `psql $DB_CONNECT_STRING -t --command "SELECT count(*) FROM pg_catalog.pg_user WHERE usename = '$ROLE_NAME'"` -eq 0 ] ; then |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
44 echo create user $ROLE_NAME |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
45 psql $DB_CONNECT_STRING --command "CREATE USER $ROLE_NAME PASSWORD '$ROLE_PW';" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
46 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
47 |
932
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
48 if [ "$DROP_DB" = "true" ] && psql $DB_CONNECT_STRING -l | grep -q "^ $DB_NAME " ; then |
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
49 echo drop db $DB_NAME |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
50 psql $DB_CONNECT_STRING --command "DROP DATABASE $DB_NAME" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
51 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
52 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
53 echo create db $DB_NAME |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
54 psql $DB_CONNECT_STRING --command \ |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
55 "CREATE DATABASE $DB_NAME WITH OWNER = $ROLE_NAME ENCODING = 'UTF8'" |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
56 |
934 | 57 echo create postgis extension |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
58 psql $DB_CONNECT_STRING -d $DB_NAME --command \ |
866
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
59 "CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public" |
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
60 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
61 echo create stammdaten schema |
1085
1d1c50b7a23b
Run psql quietly when running large SQL scripts.
Tom Gottfried <tom@intevation.de>
parents:
1076
diff
changeset
|
62 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_schema.sql |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
63 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
64 echo create lada schema |
1085
1d1c50b7a23b
Run psql quietly when running large SQL scripts.
Tom Gottfried <tom@intevation.de>
parents:
1076
diff
changeset
|
65 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_schema.sql |
1298
ba47994c1665
Add audit-trail table/trigger/views to database.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1207
diff
changeset
|
66 |
ba47994c1665
Add audit-trail table/trigger/views to database.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1207
diff
changeset
|
67 echo create audit-trail table/trigger/views |
ba47994c1665
Add audit-trail table/trigger/views to database.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1207
diff
changeset
|
68 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/audit.sql |
ba47994c1665
Add audit-trail table/trigger/views to database.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1207
diff
changeset
|
69 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
70 echo set grants |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
71 psql $DB_CONNECT_STRING -d $DB_NAME --command \ |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
72 "GRANT USAGE ON SCHEMA stammdaten, land TO $ROLE_NAME; |
764
9d2070b84238
Hibernate needs nextval to insert new datasets.
Tom Gottfried <tom@intevation.de>
parents:
752
diff
changeset
|
73 GRANT USAGE |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
74 ON ALL SEQUENCES IN SCHEMA stammdaten, land TO $ROLE_NAME; |
742
17165e710631
Add script to set up database as user postgres.
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
75 GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
76 ON ALL TABLES IN SCHEMA stammdaten, land TO $ROLE_NAME;" |
908
51051e85348f
Added data dumps.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
866
diff
changeset
|
77 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
78 if [ "$NO_DATA" != "true" ]; then |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
79 echo import stammdaten.verwaltungseinheit |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
80 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_data_verwaltungseinheit.sql |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
81 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
82 echo import stammdaten |
1191 | 83 for file in \ |
84 stammdaten_data_netzbetreiber.sql \ | |
85 stammdaten_data_mess_stelle.sql \ | |
86 stammdaten_data_auth.sql \ | |
87 stammdaten_data_mess_einheit.sql \ | |
88 stammdaten_data_umwelt.sql \ | |
89 stammdaten_data_auth_lst_umw.sql \ | |
90 stammdaten_data_datenbasis.sql \ | |
91 stammdaten_data_datensatz_erzeuger.sql \ | |
92 stammdaten_data_deskriptor_umwelt.sql \ | |
93 stammdaten_data_deskriptoren.sql \ | |
94 stammdaten_data_koordinaten_art.sql \ | |
95 stammdaten_data_messmethode.sql \ | |
96 stammdaten_data_messgroesse.sql \ | |
97 stammdaten_data_messgroessen_gruppe.sql \ | |
98 stammdaten_data_ort_typ.sql \ | |
99 stammdaten_data_staat.sql \ | |
100 stammdaten_data_kta.sql \ | |
101 stammdaten_data_ortszuordnung_typ.sql \ | |
102 stammdaten_data_pflicht_messgroesse.sql \ | |
103 stammdaten_data_proben_zusatz.sql \ | |
104 stammdaten_data_probenart.sql \ | |
105 stammdaten_data_messprogramm_transfer.sql \ | |
106 stammdaten_data_ortszusatz.sql \ | |
107 stammdaten_data_messprogramm_kategorie.sql \ | |
108 stammdaten_data_ort.sql \ | |
109 stammdaten_data_probenehmer.sql \ | |
110 stammdaten_data_query.sql \ | |
111 stammdaten_data_user_context.sql | |
112 do | |
113 echo ${file%.sql} | |
114 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/$file | |
115 done | |
116 | |
117 echo init sequences | |
118 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_init_sequences.sql | |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
119 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
120 echo import lada test data |
1085
1d1c50b7a23b
Run psql quietly when running large SQL scripts.
Tom Gottfried <tom@intevation.de>
parents:
1076
diff
changeset
|
121 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_data.sql |
1056
299f4ba86090
extend docker envireoment to load geographical data of german comunities
Michael Stanko <mstanko@bfs.de>
parents:
966
diff
changeset
|
122 |
1191 | 123 echo create schema geo |
1056
299f4ba86090
extend docker envireoment to load geographical data of german comunities
Michael Stanko <mstanko@bfs.de>
parents:
966
diff
changeset
|
124 psql $DB_CONNECT_STRING -d $DB_NAME --command "CREATE SCHEMA geo AUTHORIZATION $ROLE_NAME" |
299f4ba86090
extend docker envireoment to load geographical data of german comunities
Michael Stanko <mstanko@bfs.de>
parents:
966
diff
changeset
|
125 |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
126 echo downlaod and import german administrative borders |
1076
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
127 TS="0101" |
1207
6692b3bdc49d
Backout 2f9dfd292ff1 because it reverts d667092cc788.
Tom Gottfried <tom@intevation.de>
parents:
1203
diff
changeset
|
128 cd /tmp |
1076
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
129 if [ ! -f vg250_${TS}.utm32s.shape.ebenen.zip ]; then |
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
130 curl -O \ |
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
131 http://sg.geodatenzentrum.de/web_download/vg/vg250_${TS}/utm32s/shape/vg250_${TS}.utm32s.shape.ebenen.zip |
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
132 fi |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
133 unzip -u vg250_${TS}.utm32s.shape.ebenen.zip "*VG250_GEM*" |
1207
6692b3bdc49d
Backout 2f9dfd292ff1 because it reverts d667092cc788.
Tom Gottfried <tom@intevation.de>
parents:
1203
diff
changeset
|
134 # cd vg250_${TS}.utm32s.shape.ebenen/vg250_ebenen/ |
1193
00323d9e642c
Import german administrative borders using EPSG:4326.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1192
diff
changeset
|
135 shp2pgsql -s 25832:4326 vg250_${TS}.utm32s.shape.ebenen/vg250_ebenen/VG250_GEM geo.gem_utm | psql -q $DB_CONNECT_STRING -d $DB_NAME |
1207
6692b3bdc49d
Backout 2f9dfd292ff1 because it reverts d667092cc788.
Tom Gottfried <tom@intevation.de>
parents:
1203
diff
changeset
|
136 # rm -rf vg250_${TS}.utm32s.shape.ebenen |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
137 |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
138 echo fille stammdaten.verwaltungsgrenze |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
139 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_fill_verwaltungsgrenze.sql |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
140 |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
141 if [ -f $DIR/lada_auth.sql ]; then |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
142 echo load private auth configuration |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
143 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_auth.sql |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
144 fi |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
145 fi |