4

we are trying to restore PostgreSQL schema dumps into a database from the command line using the below command

psql -U postgres -d dbname < filename

and we are getting response in command line during the restoration like below

SET
SET
SET
SET
SET
CREATE SCHEMA
ALTER SCHEMA
.
.
.
.

and going on

My question is, if there are some errors happened in between the restoration and restoration continues to go on, how to detect that error without scrolling through these entire response through command line? or Is there anyway to automatically stop and exit from the restoration process if an error occurs?

We are using PostgreSQL 10

jithin giri
  • 733
  • 2
  • 6
  • 17

1 Answers1

6

There are two things you can do:

First run the entire script as a single transaction:

psql --single-transaction -U postgres -d dbname < filename

Additionally you can configure psql to abort if an error occurs:

\set ON_ERROR_STOP on

You would need to put the \set into the file you are running.

Alternatively, if you always want that behavior, put it into ~/.psqlrc

  • You can specify this behaviour when connecting to the database `psql --host=? --single-transaction --dbname=db --username=? --set=ON_ERROR_STOP=1` – Rafs Apr 24 '23 at 09:08