Backup & Restore
Backup and restore the Magnet AI PostgreSQL database (with pgvector) using pg_dump, psql, and pg_restore.
Connection Details
Magnet AI uses these environment variables (usually from your .env file):
DB_HOSTDB_PORTDB_NAMEDB_USERDB_PASSWORD
In the examples below we map them to the standard psql/pg_dump variables:
bash
export PGHOST="$DB_HOST"
export PGPORT="$DB_PORT"
export PGDATABASE="$DB_NAME"
export PGUSER="$DB_USER"
export PGPASSWORD="$DB_PASSWORD"Tip: prefer a
.pgpassfile (or your platform’s secret store) over exportingPGPASSWORDin long-lived shells.
Backup
Plain SQL (portable)
bash
# Backup (plain SQL)
pg_dump --no-owner --no-privileges --format=plain --file=backup.sql "$PGDATABASE"
# Optional compression
gzip -9 backup.sqlCustom format (recommended for larger databases)
bash
# Backup (custom format)
pg_dump --no-owner --no-privileges --format=custom --file=backup.dump "$PGDATABASE"Restore
Restores are destructive. Prefer restoring into a new database first, then switching your app to the new database.
Restore from plain SQL
bash
# Create the target DB if needed
createdb "$PGDATABASE"
# Restore (fails fast on errors)
psql --set ON_ERROR_STOP=on --dbname="$PGDATABASE" < backup.sqlIf you compressed the backup (backup.sql.gz), restore like this:
bash
gunzip -c backup.sql.gz | psql --set ON_ERROR_STOP=on --dbname="$PGDATABASE"Restore from custom format
bash
# Create the target DB if needed
createdb "$PGDATABASE"
# Restore (drops/recreates objects from the dump)
pg_restore --clean --if-exists --no-owner --dbname="$PGDATABASE" backup.dump