Use the \e command to edit the current query buffer. Doing so can be extremely useful when entering queries and statements in psql , as you can easily go view and mofify all lines of your query or statement before it is committed.

Note Set the EDITOR Variable
 

Before using this command, be sure that the EDITOR variable is set to the editor you wish to use, otherwise psql will use vi to edit your buffer.

You can also use this command to save your current buffer as a file. The syntax of it is essentially the same, either way: issue the \e command to enter editing mode. This will open your editor and load the buffer as if it were a file. Complete whatever work you wish to do with the buffer, then use your editor's save function to save the buffer and return to psql . To save the query as a normal file, use your editor's save-as function to save it as a file other than the .tmp that it's opened as.