Make create-db and delete-db work with DBs on other hosts

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2021-09-27 10:01:21 +02:00
parent 706805d342
commit 70a36d805f
2 changed files with 55 additions and 6 deletions

View file

@ -16,6 +16,8 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
root_path="$( cd "$(dirname "$0")" ; pwd -P )"
auth=pw
hostname=localhost
first=1
while [[ $# -gt 0 ]]
@ -29,6 +31,23 @@ case $key in
shift # past value
;;
-H|--host)
hostname="$2"
shift # past argument
shift # past value
;;
-s|--sudo)
auth=sudo
shift
;;
-p|--password)
export PGPASSWORD="$2"
shift # past argument
shift # past value
;;
-h|--help)
echo "usage: create-db <database> [OPTIONS]"
echo "Creates and prepares the database for the use with osm2city"
@ -36,6 +55,9 @@ case $key in
echo "OPTIONS"
echo " -u, --user User the database will be owned by."
echo " NOTE: If not given, the one from the general-settings will be used"
echo " -H, --host Database host name"
echo " -s, --sudo Authenticate via sudo, databases on localhost only"
echo " -p, --password Authenticate via password. Password for the postgres user"
echo " -h, --help Shows this help and exit"
exit 0
;;
@ -64,11 +86,27 @@ if [ ! -z "$man_user" ]; then
db_user="$man_user"
fi
sudo -u postgres createdb --encoding=UTF8 --owner=$db_user "$database"
sudo -u postgres psql --dbname="$database" -c "CREATE EXTENSION postgis;"
sudo -u postgres psql --dbname="$database" -c "CREATE EXTENSION hstore;"
psql -d "$database" -f "$root_path"/sql/pgsnapshot_schema_0.6.sql
psql -d "$database" -f "$root_path"/sql/pgsnapshot_schema_0.6_bbox.sql
if [ "$auth" = "pw" ]; then
if [ -z "$pw" ]; then
echo "ERROR: No password or --sudo flag given"
exit 1
fi
fi
if [ "$auth" = "sudo" ]; then
sudo -u postgres createdb --encoding=UTF8 --owner=$db_user "$database"
sudo -u postgres psql --dbname="$database" -c "CREATE EXTENSION postgis;"
sudo -u postgres psql --dbname="$database" -c "CREATE EXTENSION hstore;"
psql -d "$database" -f "$root_path"/sql/pgsnapshot_schema_0.6.sql
psql -d "$database" -f "$root_path"/sql/pgsnapshot_schema_0.6_bbox.sql
else
createdb --host="$hostname" --username=postgres --encoding=UTF8 --owner=$db_user "$database"
psql --host="$hostname" --username=postgres --dbname="$database" -c "CREATE EXTENSION postgis;"
psql --host="$hostname" --username=postgres --dbname="$database" -c "CREATE EXTENSION hstore;"
psql --host="$hostname" --username=postgres -d "$database" -f "$root_path"/sql/pgsnapshot_schema_0.6.sql
psql --host="$hostname" --username=postgres -d "$database" -f "$root_path"/sql/pgsnapshot_schema_0.6_bbox.sql
fi
if [ ! -f ".databases" ]; then
touch "$root_path"/.databases

View file

@ -16,6 +16,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
root_path="$( cd "$(dirname "$0")" ; pwd -P )"
hostname=localhost
first=1
while [[ "$#" -gt 0 ]]
@ -23,11 +24,18 @@ do
key="$1"
case $key in
-H|--host)
hostname="$2"
shift # past argument
shift # past value
;;
-h|--help)
echo "usage: delete-db <database> [OPTIONS]"
echo "Deletes the database"
echo ""
echo "OPTIONS"
echo " -H, --host Database host name"
echo " -h, --help Shows this help and exit"
exit 0
;;
@ -45,5 +53,8 @@ case $key in
esac
done
psql --dbname="postgres" -c "DROP DATABASE IF EXISTS \"$database\""
source "$root_path"/general-settings > /dev/null
export PGPASSWORD="$db_passwd"
psql --host="$hostname" --username="$db_user" --dbname="postgres" -c "DROP DATABASE IF EXISTS \"$database\""
sed -ri "s/^$key$//" "$root_path"/.databases