Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psql: warning: extra command-line argument "from" ignored

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.

like image 409
ThomasReggi Avatar asked Oct 15 '22 13:10

ThomasReggi


2 Answers

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.

like image 64
stratis Avatar answered Oct 19 '22 00:10

stratis


It looks to me like $PGDB is empty, so psql thinks --command is the name of the database.

like image 33
Jeremy Avatar answered Oct 19 '22 01:10

Jeremy