Postgresql – Can PostgreSQL results include the query

postgresqlpsql

I run the following command to run 2 queries in a sql file. Could be 1, could be 50 queries. Depends on the deployment.

psql -U <username> -h <servername> -d <databasename> -f
20140701_Queries.sql > 20140701_Results.txt

Here are the queries:

select max(day) from tbl1;
select max(day) from tbl2;

Here are the results I get:

    max
------------
 2014-06-02
(1 row)

    max
------------
 2014-06-06
(1 row)

I would rather it come out something like this:

select max(day) from tbl1;

        max
    ------------
     2014-06-02
    (1 row)

select max(day) from tbl2;
        max
    ------------
     2014-06-06
    (1 row)

But the exact formatting isn't important right now. Just that someone can look for their query and see their results.

Thanks in advance 🙂

— Edit

If I just paste the two queries into psql, I get this mix of results and my input which would be fine, if I could just output it to a file:

db=# select max(day) from tbl1;
    max
------------
 2014-06-02
(1 row)

db=# select max(day) from tbl2;
    max
------------
 2014-06-06
(1 row)

Best Answer

Use the -e flag when you start psql.
For example:

psql -e mydatabase

Or, in case of using a script, try the -a flag.
Details in the manual.