Search code examples
postgresqlpsqlpg-restorebackup-sqldatabase

How to lock Data Base when pg_restore runs


When running pg_restore we encounter some (unexpected) executions from some other programs resulting in errors in the restoration.

Is there a way to lock the DB while restoring to insure no modifications are done ?

This is the command executed :

pg_restore --clean --verbose --no-owner /home/postgres/backup/bkp --if-exists -j 7 -d restdb01 -p 5432

Solution

    • revoke connect permissions from the database or reject connections in pg_hba.conf (remember that by default, PUBLIC can connect to the database)

    • use pg_terminate_backend to cancel all existing connections

    • run pg_restore as a user that can still connect to the database