On Error Stop Psql
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings psql on_error_rollback and policies of this site About Us Learn more about Stack Overflow
Psql Continue On Error
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags on_error_stop=1 Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only psql exit code takes a minute: Sign up Postgres sql fail on script error up vote 44 down vote favorite 6 Is there a way to specify that when executing a sql script it stops when encountering the first error on the script, it usually continues, regardless of previous errors. Thanks postgresql share|improve this question asked Dec 18 '10 at 21:54 Ray 221133
Psql Error Code
add a comment| 3 Answers 3 active oldest votes up vote 67 down vote I think the solution to add following to .psqlrc is far from perfection \set ON_ERROR_STOP on there exists much more simple and convenient way - use psql with parameter: psql -v ON_ERROR_STOP=1 better to use also -x parameter turning off .psqlrc file usage. Works perfectly for me p.s. the solution found in great post from Peter Eisentraut. Thank you, Peter! http://petereisentraut.blogspot.com/2010/03/running-sql-scripts-with-psql.html share|improve this answer edited Jun 18 '13 at 12:42 answered Apr 26 '13 at 15:08 Alfishe 1,040911 3 -v ON_ERROR_STOP=ON also works, at least with 9.2. I suspect any of the variants of boolean "true" are allowed. –jpmc26 Feb 14 '14 at 11:38 It doesn't work in interactive mode, which confused me for a minute. –Sam Watkins Jul 30 '15 at 5:47 add a comment| up vote 5 down vote I assume you are using psql, this might be handy to add to your ~/.psqlrc file. \set ON_ERROR_STOP on This will make it abort on the first error. If you don't hav
buildfarm-members pgsql-cluster-hackers pgsql-committers pgsql-hackers pgsql-rrreviewers pgsql-translators pgsql-www Regional lists Associations User groups Project lists Inactive lists IRC Local User Groups Featured Users International psql run sql file Sites Propaganda Resources Weekly News psql: ON_ERROR_STOP command-line flag for scripts psql command line From: Mike Toews
Man Psql
Message-ID: AANLkTikeVP0_qA-AOb3KSNaZG4fwNe1Wh5Ls5aAsGVa6@mail.gmail.com (view raw or whole thread) Thread: 2010-06-07 16:32:41 from Mike Toews
log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn http://dba.stackexchange.com/questions/24518/how-to-conditionally-stop-a-psql-script-based-on-a-variable-value more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Database Administrators Questions Tags Users Badges Unanswered Ask Question _ Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute: Sign up Here's how it works: Anybody on error can ask a question Anybody can answer The best answers are voted up and rise to the top How to conditionally stop a psql script (based on a variable value)? up vote 5 down vote favorite Let's consider the following example (from the start of a psql script): \c :db_to_run_on TRUNCATE the_most_important_table; -- tried to avoid similarities to anything that exists out there Now if it is run this on error stop by the command psql [connection details] -v db_to_run_on=\'dev_database\' then it just runs and the user is happy. But what if (s)he decides to specify -v db_to_run_on=production_database? (Let's assume that this can happen, just like people run rm -rf / # don't try this at home!!! ocassionally.) Hopefully there is a fresh backup of that table... So the question arises: how to check the variables passed to a script and stop further processing based on their value? postgresql psql share|improve this question edited Sep 19 '12 at 8:51 asked Sep 19 '12 at 8:32 dezso 14.3k73563 psql is quite limited as a batch tool, as it lacks control structures. I'd recommend writing scripts for this sort of thing in perl/python/whatever using the appropriate database driver. –Craig Ringer Sep 19 '12 at 10:59 @CraigRinger it is, unfortunately. I have an idea now, but it needs a bit of honing yet. –dezso Sep 19 '12 at 11:13 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted There is an option in psql which stops executing commands on error, this is ON_ERROR_STOP. If we could raise an error somehow, this would do what we want. The problem i