As it is possible to use
psql
to enter queries directly from the prompt, it is possible
to create queries and statements within files and let
psql
read the files and insert
their content into the current buffer. This can be useful to some users.
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. Example 6-1 shows how to insert a
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 6-1. 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)
If you find yourself doing this often for the sole purpose of using your favorite editor, using the \edit command would probably be more convenient.