Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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)
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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 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 at 15:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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