Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop and exit from the postgresql restoration if an error occurs

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

like image 818
jithin giri Avatar asked Oct 23 '25 14:10

jithin giri


1 Answers

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