[PATCH 1 of 2] Improve Postgres-DB setup

Wald Commits scm-commit at wald.intevation.org
Thu Dec 10 17:56:36 CET 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1449765602 -3600
# Node ID c1cea6c959675031650464c36a2cc99f441c91d0
# Parent  d3804d134fd931cb38484e9bd8dac6fc9a3b2003
Improve Postgres-DB setup.

We depend on 9.1 (having CREATE EXTENSION) since a while.

diff -r d3804d134fd9 -r c1cea6c95967 backend/doc/schema/postgresql-setup.sh
--- a/backend/doc/schema/postgresql-setup.sh	Thu Dec 10 14:50:29 2015 +0100
+++ b/backend/doc/schema/postgresql-setup.sh	Thu Dec 10 17:40:02 2015 +0100
@@ -1,30 +1,32 @@
 #!/bin/bash
 
-# $1: user name and password for new DB (equals DB name)
-# $2: path to directory with schema-scripts
-# $3: host
+# $1: name, user and password for new DB (optional. Default: d4e)
+# $2: host (optional. Default: localhost)
+
+SCRIPT_DIR=`dirname $0`
+DB_NAME=${1:-d4e}
+PG_HOST=${2:-localhost}
 
 # run as user postgres (postgresql super-user)
 # it is assumed that the owner of the DB has the same name as the DB!
 
 # create PostGIS-DB
-createuser -S -D -R $1
-createdb $1
-createlang plpgsql $1
-# Path appears e.g. as /usr/share/postgresql/contrib/postgis-1.5/ on some systems,
-# /usr/share/postgresql/8.4/contrib/postgis-1.5/ on others
-POST_INIT_DIR=`find /usr/share/postgresql/ -name spatial_ref_sys.sql | sed -n '1p'`
-POST_INIT_DIR=`dirname $POST_INIT_DIR`
-psql -d $1 -f $POST_INIT_DIR/postgis.sql
-psql -d $1 -f $POST_INIT_DIR/spatial_ref_sys.sql
-psql -d $1 -c "ALTER USER $1 WITH PASSWORD '$1';"
-psql -d $1 -c "GRANT ALL ON geometry_columns TO $1; GRANT ALL ON geography_columns TO $1; GRANT ALL ON spatial_ref_sys TO $1;"
+createuser -S -D -R $DB_NAME
+createdb $DB_NAME
+createlang plpgsql $DB_NAME
+
+psql -d $DB_NAME -c "ALTER USER $DB_NAME WITH PASSWORD '$DB_NAME';"
+
+psql -d $DB_NAME -c "CREATE EXTENSION postgis;"
+psql -d $DB_NAME -c "GRANT ALL ON geometry_columns TO $DB_NAME;
+                     GRANT ALL ON geography_columns TO $DB_NAME;
+                     GRANT ALL ON spatial_ref_sys TO $DB_NAME;"
 
 # add credentials to .pgpass (or create .pgpass)
-echo "*:*:$1:$1:$1" >> ~/.pgpass
+echo "*:*:$DB_NAME:$DB_NAME:$DB_NAME" >> ~/.pgpass
 chmod 0600 ~/.pgpass
 
 # apply schema-scripts
-psql -d $1 -U $1 -h $3 -f $2/postgresql.sql
-psql -d $1 -U $1 -h $3 -f $2/postgresql-spatial.sql
-psql -d $1 -U $1 -h $3 -f $2/postgresql-minfo.sql
+psql -d $DB_NAME -U $DB_NAME -h $PG_HOST -f $SCRIPT_DIR/postgresql.sql
+psql -d $DB_NAME -U $DB_NAME -h $PG_HOST -f $SCRIPT_DIR/postgresql-spatial.sql
+psql -d $DB_NAME -U $DB_NAME -h $PG_HOST -f $SCRIPT_DIR/postgresql-minfo.sql


More information about the Dive4Elements-commits mailing list