1

I'm running a psql query like so:

$ psql --file=foo.sql "BAR-DB"

Where foo.sql contains the query. foo.sql has bad syntax, so the output is a list of error messages. My plan was to pipe the output to grep so I can filter specific error messages, but

$ psql --file=foo.sql "BAR-DB" | grep PATTERN

doesn't seem to do anything. psql's documentation shows the optional flag:

-o, --output=FILENAME    send query results to file (or |pipe)

but I'm not sure how to use it to pipe the output to grep. What is the proper syntax?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Mike S
  • 1,451
  • 1
  • 16
  • 34
  • 1
    How about `psql --file=foo.sql "BAR-DB" 2>&1 | grep -- PATTERN`? – codeforester Jun 08 '18 at 13:37
  • Perhaps this post would help you: https://stackoverflow.com/q/28436918/6862601 – codeforester Jun 08 '18 at 13:39
  • 1
    @codeforester Your first suggestion got me what I wanted! Would you mind reposting it as an answer so I could mark it answered? (and could you explain what `2>&1` and `--` are doing?) – Mike S Jun 08 '18 at 13:52

1 Answers1

2

You could redirect the stderr of psql to stdout and then pipe it to grep:

psql --file=foo.sql "BAR-DB" 2>&1 | grep -- PATTERN
codeforester
  • 39,467
  • 16
  • 112
  • 140