HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to prevent query execution after exiting the \e editor in psql?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
afterpreventthequeryeditorpsqlhowexitingexecution

Problem

Is there an option that can change the "exit external editor -> query runs automatically" behaviour of the psql \e switch ? (Other than intentionally mangling the query syntax).

Solution

Don't add a semicolon (;). I quote the manual about the \e or \edit meta-command here:

The new contents of the query buffer are then re-parsed according to
the normal rules of psql, treating the whole buffer as a single line.
Any complete queries are immediately executed; that is, if the query
buffer contains or ends with a semicolon, everything up to that point
is executed and removed from the query buffer. Whatever remains in the
query buffer is redisplayed. Type semicolon or \g to send it, or
\r to cancel it by clearing the query buffer.

To be clear: only the last statement that is not terminated with a semicolon is not executed. To execute nothing at all, make sure there are no complete statements before the last statement. I.e. no semicolons at all.

That's really unsatisfying, though. See this follow-up:

  • Reckless behavior of psql \e



.. which led Laurenz Albe to commit some improvements, live in psql 14 or later.

Context

StackExchange Database Administrators Q#31388, answer score: 9

Revisions (0)

No revisions yet.