I am trying to perform an sql query after being logged to the database. Although the Postgres official documentation mentions option c, I just keep getting error saying that command , -c, --c are not valid options.
Below is the line in my shell script file.
psql "host='localhost' port=xxxx dbname='xxxxx' user='xxxx' password='xxxxx' -c='sql string'"
CodePudding user response:
You're mixing long and short option syntax ...
This:
#!/bin/sh
echo "Connect to DB"
psql "host='localhost' port=xxxx dbname='xxxxx' user='xxxx' password='xxxxx'" -c='select * from pg_stat_archive'
needs to be:
#!/bin/sh
echo "Connect to DB"
psql "host='localhost' port=xxxx dbname='xxxxx' user='xxxx' password='xxxxx'" -c 'select * from pg_stat_archive'
The equal-sign is only used for the long-form: --command=command
CodePudding user response:
Use a here doc in your shell script to avoid the termination and behavior of the -c
psql <<EOF \x SELECT * FROM foo; EOF
See this for the specific behavior and examples https://www.postgresql.org/docs/12/app-psql.html
CodePudding user response:
-c option has to to put in the front based on the comment by @Charles Duffy
#!/bin/sh echo "Connect to DB" psql -c 'select * from any_table' "host='localhost' port=xxxx dbname='xxxxx' user='xxxx' password='xxxxx'"