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).
Don't add a semicolon (
;). I quote the manual about the
\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
\gto send it, or
\rto 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:
.. which led Laurenz Albe to commit some improvements, live in psql 14 or later.
If you have many request and some semicolons use Transaction :
In psql open editor mode
-- at start of edited text BEGIN TRANSACTION; -- ... your requests here -- save exit
Back to psql : your requests are executed but change are not written :
If all is OK, save changes with :
else If you have errors or want to cancel :