2
0

load_database.sh 766 B

1234567891011121314151617181920
  1. #!/bin/sh
  2. # Drop, re-create and load a local NetBox database from a SQL dump, granting
  3. # the role named after the database create on it and on schema public. The
  4. # schema grant is required since PostgreSQL 15. Runs psql as the postgres
  5. # superuser via sudo.
  6. #
  7. # Usage: scripts/load_database.sh <dump.sql> [database=netbox]
  8. # Example: scripts/load_database.sh netbox.sql
  9. DB=${2:-netbox}
  10. sudo -u postgres psql -c "DROP DATABASE $DB"
  11. sudo -u postgres psql -c "CREATE DATABASE $DB"
  12. sudo -u postgres psql -c "GRANT CREATE ON DATABASE $DB TO $DB"
  13. # Load tables from the production dump
  14. sudo -u postgres psql $DB < $1
  15. # Grant after the load so a dump that re-creates schema public cannot undo it
  16. sudo -u postgres psql -d $DB -c "GRANT CREATE ON SCHEMA public TO $DB"