How to Spool Output to a File in PostgreSQL using psql
Table of Contents
1. Oracle SPOOL and psql Equivalents
In Oracle SQL*Plus you run spool file, run your commands, then spool off. PostgreSQL has no SPOOL command. In psql the same result is done with \o (output to a file) and \o with no file name (stop, same as spool off).
| Oracle SQL*Plus | PostgreSQL psql | Notes |
|---|---|---|
spool /tmp/a.log |
\o /tmp/a.log |
Query results go to the file, not to the screen. The file is overwritten. |
spool off |
\o |
Stops writing to the file. Output comes back to the screen. |
spool a.log append |
\o | tee -a /tmp/a.log |
Appends to the file, and also shows the output on the screen. |
prompt text |
\qecho text |
Writes your own text into the spool file. |
host cat file |
\! cat file |
Runs an OS command from inside psql. |
2. Spool with \o and stop with \o
After \o /tmp/spool.log, the query results are written to the file, so you do not see them on the screen. The SQL you typed is not written to the file.
[postgres@pglab01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres
psql (17.12)
Type "help" for help.
postgres=# \o /tmp/spool.log <---- spool on
postgres=# select current_user; <---- no output on the screen, it goes to the file
postgres=# select now();
postgres=# select count(*) from pg_database;
postgres=# \o <---- spool off
postgres=#
postgres=# \! cat /tmp/spool.log <---- check the file from inside psql
current_user
--------------
postgres
(1 row)
now
-------------------------------
2026-11-30 11:45:10.123456+00
(1 row)
count
-------
4
(1 row)
postgres=#
\o fileoverwrites the file every time. Use a new file name for each run, or use thetee -amethod in section 3.- Only query results are written. Your SQL text, error messages and the output of
\commands (like\conninfoor\l) are not in the file, unless you use\qechoor the methods in sections 6 and 7.
3. Append to the file and also show on the screen (\o | tee -a)
\o | tee -a file sends the output to the tee command, which writes it to the file in append mode and also prints it on the screen. \qecho writes your own heading into the same output.
postgres=# \o | tee -a /tmp/spool_tee.log
postgres=# \qecho ===== Check 1 : current user =====
===== Check 1 : current user =====
postgres=# select current_user;
current_user
--------------
postgres
(1 row)
postgres=# \qecho ===== Check 2 : server version =====
===== Check 2 : server version =====
postgres=# show server_version;
server_version
----------------
17.12
(1 row)
postgres=# \o <---- spool off
postgres=# \! cat /tmp/spool_tee.log
===== Check 1 : current user =====
current_user
--------------
postgres
(1 row)
===== Check 2 : server version =====
server_version
----------------
17.12
(1 row)
postgres=#
Run the same \o | tee -a /tmp/spool_tee.log again later, and the new output is added to the end of the file.
4. One query only (\g file)
\g replaces the ; and sends the result of that one query to a file. Use \g | tee -a to append and also see it on the screen.
postgres=# select now() \g /tmp/now.log
postgres=# select now() \g | tee -a /tmp/now_tee.log
now
-------------------------------
2026-11-30 11:50:22.654321+00
(1 row)
postgres=# \! cat /tmp/now.log
now
-------------------------------
2026-11-30 11:50:20.111111+00
(1 row)
postgres=#
5. Log the output from the command line (-L and -o)
# -L : write query output to the file AND show it on the screen
[postgres@pglab01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres -L /tmp/psql_session.log
postgres=# select current_user;
current_user
--------------
postgres
(1 row)
postgres=# \q
[postgres@pglab01 ~]$ cat /tmp/psql_session.log
# -o : write query output only to the file (nothing on the screen)
[postgres@pglab01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres -o /tmp/out.log -c "select current_user;"
[postgres@pglab01 ~]$ cat /tmp/out.log
current_user
--------------
postgres
(1 row)
[postgres@pglab01 ~]$
The content of the -L file can differ by version, so please check it on your server.
6. Run a script and spool the result, with errors (-f, -a, redirect)
This is the best way for a script, because you can also capture the SQL text and the errors. Example script:
[postgres@pglab01 ~]$ cat /tmp/script.sql
select current_user;
select * from table_not_exists;
select now();
Without -a, only the results are written. With 2>&1, the error messages are also written:
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -f /tmp/script.sql > /tmp/out1.log 2>&1
[postgres@pglab01 ~]$ cat /tmp/out1.log
current_user
--------------
postgres
(1 row)
psql:/tmp/script.sql:2: ERROR: relation "table_not_exists" does not exist
LINE 1: select * from table_not_exists;
^
now
-------------------------------
2026-11-30 11:55:31.222222+00
(1 row)
[postgres@pglab01 ~]$
Add -a to write each SQL statement into the file before its result, so the file reads like an Oracle spool of a script:
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -a -f /tmp/script.sql > /tmp/out2.log 2>&1
[postgres@pglab01 ~]$ cat /tmp/out2.log
select current_user;
current_user
--------------
postgres
(1 row)
select * from table_not_exists;
psql:/tmp/script.sql:2: ERROR: relation "table_not_exists" does not exist
LINE 1: select * from table_not_exists;
^
select now();
now
-------------------------------
2026-11-30 11:55:45.333333+00
(1 row)
[postgres@pglab01 ~]$
Inside a script file you can put the spool commands at the start and the end, just like in Oracle:
[postgres@pglab01 ~]$ cat /tmp/script_spool.sql
\o /tmp/script_spool.log
\qecho ===== Report started =====
select current_user;
select now();
\o
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -f /tmp/script_spool.sql
[postgres@pglab01 ~]$ cat /tmp/script_spool.log
===== Report started =====
current_user
--------------
postgres
(1 row)
now
-------------------------------
2026-11-30 11:58:02.444444+00
(1 row)
[postgres@pglab01 ~]$
7. Capture the whole session including the SQL you typed (script command)
If you want the commands you typed, the results and the errors in one file, use the Linux script command around the psql session:
[postgres@pglab01 ~]$ script -a /tmp/session.log
Script started, output log file is '/tmp/session.log'.
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres
postgres=# select current_user;
current_user
--------------
postgres
(1 row)
postgres=# select * from table_not_exists;
ERROR: relation "table_not_exists" does not exist
LINE 1: select * from table_not_exists;
^
postgres=# \q
[postgres@pglab01 ~]$ exit
Script done.
[postgres@pglab01 ~]$ cat /tmp/session.log
scriptrecords everything shown on the terminal, so the file also has the prompts and your typed commands.- Do not type a password while
scriptis running, because it is not shown on the screen but it can still be recorded in some cases. Use.pgpassinstead.