How to enable quiet mode for Postgres commands on

2019-07-21 11:07发布

问题:

When using the psql command line utility on my local machine, I have the option to use the -q or --quiet switch to tell Postgres to do it's work quietly - i.e. it won't print every single INSERT statement to the console if you're doing a large import.

Here's an example of how I'm using it:

psql -q -d <SOME_DATABASE> -f <SOME_SQL_FILE>

However, when using the pg:psql command line utility in Heroku, that option doesn't seem to be available. So I'm currently having to use it like so:

heroku pg:psql DATABASE -a <SOME_HEROKU_APP> < <SOME_SQL_FILE>

which produces a lot of output to my console (hundreds of thousands of lines), because of the large size of the SQL file I'm importing. Whenever I try to use the -q or --quiet option, something like this:

heroku pg:psql DATABASE -q -a <SOME_HEROKU_APP> < <SOME_SQL_FILE>

it'll throw an error saying that -q is not a valid option.

Is there some way to enable quiet mode when running Postgres commands in Heroku?

回答1:

heroku pg:psql is just a wrapper onto your local psql binary (https://github.com/heroku/heroku/blob/master/lib/heroku/command/pg.rb#L151)

So, given this - you are able to do:

psql `heroku config:get DATABASE_URL -a <yourappname>`

to get a psql connection and consequently pass -q other options accordingly.