0

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

2 Answers2

1

PostgreSQL documentation of pg_restore does not provide any info of such functionality.

According to wiki postgresql does not support database locking.

I suggest using permission mechanism to prevent anyone connecting to the database beeing restored:

  1. Create a new database.
  2. Grant CONNECT permission to that database to a single user used in pg_restore command (--username=db-admin).
  3. Restore.
  4. Add all the required permissions to the database.

Unless you have applications connecting to your server as superusers, that will prevent applications from reading or changing

Julius Tuskenis
  • 1,323
  • 8
  • 13
1
  • 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

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263