diff --git a/scripts/drop_database.sh b/scripts/drop_database.sh index fdb2ba154..4c0bf2cbd 100755 --- a/scripts/drop_database.sh +++ b/scripts/drop_database.sh @@ -1,7 +1,14 @@ #!/bin/sh +# Drop and re-create a local NetBox database, granting the role named after +# it create on the database and on schema public. The schema grant is +# required since PostgreSQL 15. Runs psql as the postgres superuser via sudo. +# +# Usage: scripts/drop_database.sh [database=netbox] +# Example: scripts/drop_database.sh DB=${1:-netbox} -# Drop and re-create the database locally sudo -u postgres psql -c "drop database $DB" sudo -u postgres psql -c "create database $DB" +sudo -u postgres psql -c "grant create on database $DB to $DB" +sudo -u postgres psql -d $DB -c "grant create on schema public to $DB" diff --git a/scripts/load_database.sh b/scripts/load_database.sh index 4fab3420d..9f859a3e2 100755 --- a/scripts/load_database.sh +++ b/scripts/load_database.sh @@ -1,11 +1,20 @@ #!/bin/sh +# Drop, re-create and load a local NetBox database from a SQL dump, granting +# the role named after the database create on it and on schema public. The +# schema grant is required since PostgreSQL 15. Runs psql as the postgres +# superuser via sudo. +# +# Usage: scripts/load_database.sh [database=netbox] +# Example: scripts/load_database.sh netbox.sql DB=${2:-netbox} -# Drop and re-create the database locally sudo -u postgres psql -c "DROP DATABASE $DB" sudo -u postgres psql -c "CREATE DATABASE $DB" sudo -u postgres psql -c "GRANT CREATE ON DATABASE $DB TO $DB" # Load tables from the production dump sudo -u postgres psql $DB < $1 + +# Grant after the load so a dump that re-creates schema public cannot undo it +sudo -u postgres psql -d $DB -c "GRANT CREATE ON SCHEMA public TO $DB" diff --git a/scripts/save_database.sh b/scripts/save_database.sh index 4f200877b..500522208 100755 --- a/scripts/save_database.sh +++ b/scripts/save_database.sh @@ -1,7 +1,12 @@ #!/bin/sh +# Dump a local NetBox database to a SQL file that load_database.sh can +# restore. Runs pg_dump as the postgres superuser via sudo, but the dump +# file is written by the invoking user. +# +# Usage: scripts/save_database.sh [database=netbox] +# Example: scripts/save_database.sh netbox.sql DB=${2:-netbox} -# Dump the database to a file sudo -u postgres pg_dump $DB > $1