tom@933: #!/bin/sh -e mstanko@930: # SYNOPSIS tom@966: # ./setup-db.sh [-cn] [ROLE_NAME] [ROLE_PW] [DB_NAME] mstanko@930: # -c clean - drop an existing database tom@966: # -n no data - do not import example data tom@934: # ROLE_NAME name of db user (default = lada) mstanko@930: # ROLE_PW login password (default = ROLE_NAME) mstanko@930: # DB_NAME name of the databaes (default = ROLE_NAME) mstanko@930: # tom@934: # There will be used a remote database server if there exists the tom@934: # enviroment variable DB_SRV and optional DB_PORT mstanko@930: tom@742: DIR=`dirname $0` tom@742: tom@966: while getopts "cn" opt; do tom@932: case "$opt" in tom@932: c) tom@932: DROP_DB="true" tom@932: ;; tom@966: n) tom@966: NO_DATA="true" tom@966: ;; tom@932: esac tom@932: done tom@932: tom@932: shift $((OPTIND-1)) mstanko@930: tom@854: ROLE_NAME=${1:-lada} tom@934: echo "ROLE_NAME = $ROLE_NAME" tom@854: ROLE_PW=${2:-$ROLE_NAME} mstanko@925: echo "ROLE_PW = $ROLE_PW" tom@854: DB_NAME=${3:-$ROLE_NAME} mstanko@925: echo "DB_NAME = $DB_NAME" tom@742: mstanko@925: # if variable DB_SRV and otional DB_PORT is set a remote database connection will be used mstanko@925: if [ -n "$DB_SRV" ] ; then DB_CONNECT_STRING="-h $DB_SRV" ; fi tom@934: if [ -n "$DB_SRV" -a -n "$DB_PORT" ] ; then mstanko@925: DB_CONNECT_STRING="$DB_CONNECT_STRING -p $DB_PORT" mstanko@925: fi mstanko@925: DB_CONNECT_STRING="$DB_CONNECT_STRING -U postgres" mstanko@928: echo "DB_CONNECT_STRING = $DB_CONNECT_STRING" tom@866: mstanko@925: if [ `psql $DB_CONNECT_STRING -t --command "SELECT count(*) FROM pg_catalog.pg_user WHERE usename = '$ROLE_NAME'"` -eq 0 ] ; then mstanko@925: echo create user $ROLE_NAME mstanko@925: psql $DB_CONNECT_STRING --command "CREATE USER $ROLE_NAME PASSWORD '$ROLE_PW';" mstanko@925: fi mstanko@925: tom@932: if [ "$DROP_DB" = "true" ] && psql $DB_CONNECT_STRING -l | grep -q "^ $DB_NAME " ; then tom@932: echo drop db $DB_NAME mstanko@925: psql $DB_CONNECT_STRING --command "DROP DATABASE $DB_NAME" mstanko@925: fi mstanko@925: mstanko@925: echo create db $DB_NAME mstanko@925: psql $DB_CONNECT_STRING --command \ mstanko@925: "CREATE DATABASE $DB_NAME WITH OWNER = $ROLE_NAME ENCODING = 'UTF8'" mstanko@925: tom@934: echo create postgis extension mstanko@925: psql $DB_CONNECT_STRING -d $DB_NAME --command \ tom@866: "CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public" tom@866: mstanko@925: echo create stammdaten schema tom@1085: psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_schema.sql mstanko@925: mstanko@925: echo create lada schema tom@1085: psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_schema.sql mstanko@925: echo set grants mstanko@925: psql $DB_CONNECT_STRING -d $DB_NAME --command \ tom@742: "GRANT USAGE ON SCHEMA stammdaten, bund, land TO $ROLE_NAME; tom@764: GRANT USAGE raimund@752: ON ALL SEQUENCES IN SCHEMA stammdaten, bund, land TO $ROLE_NAME; tom@742: GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES tom@742: ON ALL TABLES IN SCHEMA stammdaten, bund, land TO $ROLE_NAME;" raimund@908: tom@966: if [ "$NO_DATA" != "true" ]; then tom@966: echo import stammdaten tom@1085: psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/stammdaten_data.sql mstanko@925: tom@966: echo import lada test data tom@1085: psql -q $DB_CONNECT_STRING -d $DB_NAME -f $DIR/lada_data.sql mstanko@1056: mstanko@1056: echo create user $ROLE_NAME mstanko@1056: psql $DB_CONNECT_STRING -d $DB_NAME --command "CREATE SCHEMA geo AUTHORIZATION $ROLE_NAME" mstanko@1056: tom@1076: TS="0101" mstanko@1056: cd /tmp tom@1076: if [ ! -f vg250_${TS}.utm32s.shape.ebenen.zip ]; then tom@1076: curl -O \ tom@1076: http://sg.geodatenzentrum.de/web_download/vg/vg250_${TS}/utm32s/shape/vg250_${TS}.utm32s.shape.ebenen.zip tom@1076: fi mstanko@1056: unzip vg250_${TS}.utm32s.shape.ebenen.zip "*VG250_GEM*" mstanko@1056: cd vg250_${TS}.utm32s.shape.ebenen/vg250_ebenen/ tom@1085: shp2pgsql VG250_GEM geo.gem_utm | psql -q $DB_CONNECT_STRING -d $DB_NAME mstanko@1056: cd /tmp tom@1076: rm -rf vg250_${TS}.utm32s.shape.ebenen tom@966: fi