Mercurial > lada > lada-server
annotate db_schema/setup-db.sh @ 1340:372f7f679340
changed lada-server version to 2.6-SNAPSHOT, because 2.6.1 should only point to one specific commit
author | Marco Lechner, Bundesamt fuer Strahlenschutz, SW 2.1 <mlechner@bfs.de> |
---|---|
date | Fri, 07 Apr 2017 09:59:46 +0200 |
parents | 46aa15c8cfb2 |
children |
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 |
1313
b5ab25b7ac2c
Stop on error any execution of SQL via psql in DB setup.
Tom Gottfried <tom@intevation.de>
parents:
1298
diff
changeset
|
35 # Stop on error any execution of SQL via psql |
b5ab25b7ac2c
Stop on error any execution of SQL via psql in DB setup.
Tom Gottfried <tom@intevation.de>
parents:
1298
diff
changeset
|
36 DB_CONNECT_STRING="-v ON_ERROR_STOP=on " |
b5ab25b7ac2c
Stop on error any execution of SQL via psql in DB setup.
Tom Gottfried <tom@intevation.de>
parents:
1298
diff
changeset
|
37 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
38 # 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
|
39 if [ -n "$DB_SRV" ] ; then DB_CONNECT_STRING="-h $DB_SRV" ; fi |
934 | 40 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
|
41 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
|
42 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
43 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
|
44 echo "DB_CONNECT_STRING = $DB_CONNECT_STRING" |
866
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
45 |
1319 | 46 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
|
47 echo create user $ROLE_NAME |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
48 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
|
49 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
50 |
932
f5d5cc08966a
Remove wrong test operator and prepare for more options.
Tom Gottfried <tom@intevation.de>
parents:
931
diff
changeset
|
51 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
|
52 echo drop db $DB_NAME |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
53 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
|
54 fi |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
55 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
56 echo create db $DB_NAME |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
57 psql $DB_CONNECT_STRING --command \ |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
58 "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
|
59 |
934 | 60 echo create postgis extension |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
61 psql $DB_CONNECT_STRING -d $DB_NAME --command \ |
866
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
62 "CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public" |
d47e6b8f3897
Reorganise database setup scripts.
Tom Gottfried <tom@intevation.de>
parents:
854
diff
changeset
|
63 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
64 echo create stammdaten 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/stammdaten_schema.sql |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
66 |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
67 echo create lada schema |
1085
1d1c50b7a23b
Run psql quietly when running large SQL scripts.
Tom Gottfried <tom@intevation.de>
parents:
1076
diff
changeset
|
68 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
|
69 |
ba47994c1665
Add audit-trail table/trigger/views to database.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1207
diff
changeset
|
70 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
|
71 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
|
72 |
925
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
73 echo set grants |
2b746f4e97d6
fix db-setup and stammdaten_data.sql
Michael Stanko <mstanko@bfs.de>
parents:
908
diff
changeset
|
74 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
|
75 "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
|
76 GRANT USAGE |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
77 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
|
78 GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES |
1097
186d602e031a
Merged branch schema-update into default.
Tom Gottfried <tom@intevation.de>
parents:
1085
diff
changeset
|
79 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
|
80 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
81 if [ "$NO_DATA" != "true" ]; then |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
82 echo import stammdaten.verwaltungseinheit |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
83 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
|
84 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
85 echo import stammdaten |
1191 | 86 for file in \ |
87 stammdaten_data_netzbetreiber.sql \ | |
88 stammdaten_data_mess_stelle.sql \ | |
89 stammdaten_data_auth.sql \ | |
90 stammdaten_data_mess_einheit.sql \ | |
91 stammdaten_data_umwelt.sql \ | |
92 stammdaten_data_auth_lst_umw.sql \ | |
93 stammdaten_data_datenbasis.sql \ | |
94 stammdaten_data_datensatz_erzeuger.sql \ | |
95 stammdaten_data_deskriptor_umwelt.sql \ | |
96 stammdaten_data_deskriptoren.sql \ | |
97 stammdaten_data_koordinaten_art.sql \ | |
98 stammdaten_data_messmethode.sql \ | |
99 stammdaten_data_messgroesse.sql \ | |
100 stammdaten_data_messgroessen_gruppe.sql \ | |
101 stammdaten_data_ort_typ.sql \ | |
102 stammdaten_data_staat.sql \ | |
103 stammdaten_data_kta.sql \ | |
104 stammdaten_data_ortszuordnung_typ.sql \ | |
105 stammdaten_data_pflicht_messgroesse.sql \ | |
106 stammdaten_data_proben_zusatz.sql \ | |
107 stammdaten_data_probenart.sql \ | |
108 stammdaten_data_messprogramm_transfer.sql \ | |
109 stammdaten_data_ortszusatz.sql \ | |
110 stammdaten_data_messprogramm_kategorie.sql \ | |
111 stammdaten_data_ort.sql \ | |
112 stammdaten_data_probenehmer.sql \ | |
113 stammdaten_data_query.sql \ | |
114 stammdaten_data_user_context.sql | |
115 do | |
116 echo ${file%.sql} | |
117 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/$file | |
118 done | |
119 | |
120 echo init sequences | |
121 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
|
122 |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
123 echo import lada test data |
1085
1d1c50b7a23b
Run psql quietly when running large SQL scripts.
Tom Gottfried <tom@intevation.de>
parents:
1076
diff
changeset
|
124 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
|
125 |
1320 | 126 echo import lada messprogramm |
127 psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_messprogramm.sql | |
128 | |
1191 | 129 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
|
130 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
|
131 |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
132 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
|
133 TS="0101" |
1207
6692b3bdc49d
Backout 2f9dfd292ff1 because it reverts d667092cc788.
Tom Gottfried <tom@intevation.de>
parents:
1203
diff
changeset
|
134 cd /tmp |
1076
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
135 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
|
136 curl -O \ |
4ce4bd48c260
Avoid repeating potentially lengthy download of shapefiles.
Tom Gottfried <tom@intevation.de>
parents:
1056
diff
changeset
|
137 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
|
138 fi |
1188
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
139 unzip -u vg250_${TS}.utm32s.shape.ebenen.zip "*VG250_GEM*" |
1319 | 140 |
1193
00323d9e642c
Import german administrative borders using EPSG:4326.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
1192
diff
changeset
|
141 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
|
142 |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
143 echo fille stammdaten.verwaltungsgrenze |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
144 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
|
145 |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
146 if [ -f $DIR/lada_auth.sql ]; then |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
147 echo load private auth configuration |
1bc8ab13e1f7
redesign stammdaten.verwaltungseinheit
Michael Stanko <mstanko@bfs.de>
parents:
1097
diff
changeset
|
148 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
|
149 fi |
966
bc44dcda6f69
Add option to create database with schema only.
Tom Gottfried <tom@intevation.de>
parents:
934
diff
changeset
|
150 fi |