I am trying to execute a non-interactive postgres command.
PGPASSWORD=$PGPASS psql -h 127.0.0.1 -U postgresql -d $PGDB --command "select count(*) from services"
Which has been giving me this response.
psql: warning: extra command-line argument "from" ignored
psql: warning: extra command-line argument "services;" ignored
psql: warning: extra command-line argument "mydbname" ignored
psql: FATAL: database "count(*)" does not exist
I've read that this could be because the terminal / bash is trying to break up each argument to --command
/ -c
as it's own argument.
I've also tried this:
PSQLARGS=(-h 127.0.0.1 -U postgresql -c )
PSQLARGS+=("select count(*) from services;")
PSQLARGS+=(${PGDB})
PGPASSWORD=$PGPASS psql "${PSQLARGS[@]}"
Some way of forcing the terminal to know it's one argument, this also didn't work.
This is a pretty cheap and common error. Putting everything in quotes should do the trick.
Try this:
PGPASSWORD="$PGPASS" psql -h '127.0.0.1' -U 'postgresql' -d "$PGDB" --command "select count(*) from services"
Let us know if it works for you.
It looks to me like $PGDB is empty, so psql thinks --command is the name of the database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With