As it is possible to use psql to enter queries from the prompt, it is also possible to create queries and statements within files, then let psql read the files and insert their content into the current buffer. This can be useful to some users; however, if you find yourself doing this often for the sole purpose that you are able to use your favorite editor, the use of the \edit command would probably be more convenient.

To use this feature, first enter your query into a file on your filesystem; after it is complete, open psql . The command to insert files into the current query buffer is \i . The next example shows how to insert a simple simple file-based SELECT command into the current buffer. Text from the query is displayed on-screen in the example, but this may not happen on your system by default. To see the lines of a file as it is being read, set the ECHO variable to 'all' by typing \set ECHO all .

Example 4-50. Inserting a File into the Current Buffer

testdb=# \set ECHO all
testdb=# \i /usr/local/pgsql/query
SELECT * FROM employees WHERE firstname='Andrew';
 firstname | lastname | id
 Andrew    | Brookins | 100
 (1 row)