Using Visual Studio Code with psql

A quick HOWTO to show you how to setup psql to use Visual Studio Code for interactive editing.

Updating your EDITOR

When you're working in psql, it can be handy to jump to a richer text editor, particularly when typing up longer queries. In psql, you can do this by typing \e which will open a text editor with the last query that was run, ready for editing. And on saving and closing the editor, the query you were working on in the editor will also be run.

To decide what text editor to launch, psql looks for the PSQL_EDITOR environment variable. So to use Visual Studio Code, update your .bashrc (or equivalent) to add the following line

export PSQL_EDITOR="code -w"

The -w flag is important. It causes psql to wait for you to close the file in VS Code before returning.