0

I can successfully run SQL (Postgres) files from command line following instructions here:

Run a PostgreSQL .sql file using command line arguments

In particular, I use something like

psql -d DBPASSWORD -a -f FILENAME

Problem is that this (and specifically, I believe the -a) prints the sql code out to the terminal. This is annoying because I am running a lot of files in sequence within a Python script using subprocess, and I would rather not have the SQL code print out in terminal. Is there a way to not print the SQL code out to terminal?

EDIT: I've tried adding the -q option like people said, but the code in the SQL file is still being printed out to terminal.

What I tried was

psql -q -d DBPASSWORD -a -f FILENAME 
psql -d DBPASSWORD -q -a -f FILENAME 
psql -d DBPASSWORD -a -q -f FILENAME 
psql -d DBPASSWORD -a -f FILENAME -q

And in each of those cases, the code in FILENAME is being printed to terminal

Community
  • 1
  • 1
Vincent
  • 7,808
  • 13
  • 49
  • 63
  • What OS are you running? If it is some kind of UNIX you can redirect output to /dev/null like 'psql -d foo > /dev/null'. – kometen Apr 21 '16 at 14:27
  • 1
    `--quiet` or `-q` should do it: http://www.postgresql.org/docs/9.3/static/app-psql.html – Javier Buzzi Apr 21 '16 at 14:28
  • maybe the -q / -quiet option? – J J Apr 21 '16 at 14:28
  • @Vincent That'll fall into the category. :-) But maybe -q is better as others have suggested. – kometen Apr 21 '16 at 14:30
  • Doesn't seem to work? I've tried `psql -q -d DBPASSWORD -a -f FILENAME` `psql -d DBPASSWORD -q -a -f FILENAME` `psql -d DBPASSWORD -a -q -f FILENAME` `psql -d DBPASSWORD -a -f FILENAME -q` to no avail. – Vincent Apr 21 '16 at 14:37
  • What exactly is "not working". What kind of output do you still get? What is the content of the file? Please [edit] your question and provide that information. Do not post code in comments. –  Apr 21 '16 at 14:42
  • Then try to redirect to/dev/null as I suggested. – kometen Apr 21 '16 at 15:09
  • `-a` or `--echo-all` option [Print all nonempty input lines to standard output as they are read.](http://www.postgresql.org/docs/current/static/app-psql.html) So what is your actual goal? – Abelisto Apr 21 '16 at 15:35
  • My goal is to the a SQL query from command line where I just have to specify the filename and I don't want to print the code to terminal. – Vincent Apr 21 '16 at 17:39
  • So just remove the `-a` option. – Abelisto Apr 21 '16 at 17:43
  • Oh great! I think that works. – Vincent Apr 25 '16 at 21:40

1 Answers1

0

You may want to redirect STDOUT, STDERR or both to a log file.

Something like one of these

  • psql ... > out.log
  • psql ... 2> err.log
  • psql ... &> out_and_err.log
Clément Prévost
  • 8,000
  • 2
  • 36
  • 51
  • Interesting. So if I run `psql -d DBPASSWORD -f FILENAME -q &> out_and_err.log` that will redirect both `STDOUT` and `STDERR` to a log file? – Vincent Apr 25 '16 at 21:39
  • Exactly, you don't even need the `-q` option to get the full details in the log file. You can also combine `>` and `2>` to have errors in one file and regular output in another file. – Clément Prévost Apr 25 '16 at 21:47
  • `psql -d DBPASSWORD -f FILENAME -q &> out_and_err.log` gave the error TypeError: bufsize must be an integer – Vincent May 03 '16 at 16:53