Mercurial > lada > lada-server
annotate db_schema/setup-db.sh @ 1320:bfbbd390c9a8 release-2.5
update queries
author | Michael Stanko <mstanko@bfs.de> |
---|---|
date | Thu, 23 Mar 2017 11:27:36 +0100 |
parents | 512616ea3228 |
children | 22c9bbb88d1a |
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 |
1319 | 43 if [ `psql $DB_CONNECT_STRING -t --quiet --command "SELECT count(*) FROM pg_catalog.pg_user WHERE usename = '$ROLE_NAME'"` -eq 0 ] ; then |
925
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 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
66 echo set grants |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
67 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
|
68 "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
|
69 GRANT USAGE |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
70 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
|
71 GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
72 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
|
73 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
74 if [ "$NO_DATA" != "true" ]; then |
1319 | 75 if [ -f $DIR/lada_private_data.zip ]; then |
76 echo inclue private data | |
77 unzip -o lada_private_data.zip | |
78 fi | |
79 | |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
80 echo import stammdaten.verwaltungseinheit |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
81 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
|
82 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
83 echo import stammdaten |
1191 | 84 for file in \ |
85 stammdaten_data_netzbetreiber.sql \ | |
86 stammdaten_data_mess_stelle.sql \ | |
87 stammdaten_data_auth.sql \ | |
88 stammdaten_data_mess_einheit.sql \ | |
89 stammdaten_data_umwelt.sql \ | |
90 stammdaten_data_auth_lst_umw.sql \ | |
91 stammdaten_data_datenbasis.sql \ | |
92 stammdaten_data_datensatz_erzeuger.sql \ | |
93 stammdaten_data_deskriptor_umwelt.sql \ | |
94 stammdaten_data_deskriptoren.sql \ | |
95 stammdaten_data_koordinaten_art.sql \ | |
96 stammdaten_data_messmethode.sql \ | |
97 stammdaten_data_messgroesse.sql \ | |
98 stammdaten_data_messgroessen_gruppe.sql \ | |
99 stammdaten_data_ort_typ.sql \ | |
100 stammdaten_data_staat.sql \ | |
101 stammdaten_data_kta.sql \ | |
102 stammdaten_data_ortszuordnung_typ.sql \ | |
103 stammdaten_data_pflicht_messgroesse.sql \ | |
104 stammdaten_data_proben_zusatz.sql \ | |
105 stammdaten_data_probenart.sql \ | |
106 stammdaten_data_messprogramm_transfer.sql \ | |
107 stammdaten_data_ortszusatz.sql \ | |
108 stammdaten_data_messprogramm_kategorie.sql \ | |
109 stammdaten_data_ort.sql \ | |
110 stammdaten_data_probenehmer.sql \ | |
111 stammdaten_data_query.sql \ | |
112 stammdaten_data_user_context.sql | |
113 do | |
114 echo ${file%.sql} | |
115 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/$file | |
116 done | |
117 | |
118 echo init sequences | |
119 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
|
120 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
121 echo import lada test data |
1085
1d1c50b7a23b
Run psql quietly when running large SQL scripts.
Tom Gottfried <tom@intevation.de>
parents:
1076
diff
changeset
|
122 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
|
123 |
1320 | 124 echo import lada messprogramm |
125 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_messprogramm.sql | |
126 | |
1191 | 127 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
|
128 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
|
129 |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
130 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
|
131 TS="0101" |
1207
6692b3bdc49d
Backout 2f9dfd292ff1 because it reverts d667092cc788.
Tom Gottfried <tom@intevation.de>
parents:
1203
diff
changeset
|
132 cd /tmp |
1076
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
133 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
|
134 curl -O \ |
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
135 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
|
136 fi |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
137 unzip -u vg250_${TS}.utm32s.shape.ebenen.zip "*VG250_GEM*" |
1319 | 138 |
1193
00323d9e642c
Import german administrative borders using EPSG:4326.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1192
diff
changeset
|
139 shp2pgsql -s 25832:4326 vg250_${TS}.utm32s.shape.ebenen/vg250_ebenen/VG250_GEM geo.gem_utm | psql -q $DB_CONNECT_STRING -d $DB_NAME |
1188
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 echo fille stammdaten.verwaltungsgrenze |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
142 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
|
143 |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
144 if [ -f $DIR/lada_auth.sql ]; then |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
145 echo load private auth configuration |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
146 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
|
147 fi |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
148 fi |