0

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)
3

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.

  • psql -U <username> -h <servername> -d <databasename> -f 20140701_Queries.sql > 20140701_Results.txt -e That does it. I expected there was an option, I just wasn't finding it. Thank you very much! – ShawnMTherrien Jul 1 '14 at 22:45
  • Glad to help, @ShawnMTherrien! I learned something myself. Thanks for the edit, @Erwin Brandstetter. As you've noticed, I'm new here. – Daniel Wilson Jul 2 '14 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.