Unable to execute a command in Psql from the Terminal Unable to execute a command in Psql from the Terminal unix unix

Unable to execute a command in Psql from the Terminal


psql --help gives (in part)

-c, --command=COMMAND    run only single command (SQL or internal) and exit

You have two commands here, to get both results you need to combine the queries (a UNION); like

psql -d data1 -U user123 -c \  "select count(*) from table1 UNION ALL select count(*) from table2;"

Or, run two commands (queries)

psql -d data1 -U user123 -c "select count(*) from table1;"psql -d data1 -U user123 -c "select count(*) from table2;"

Or, you can put both commands in a file and use -f (psql --help says)

-f, --file=FILENAME      execute commands from file, then exit


Use pipe:

$ echo "> select 1;> \d> " | psql # here is the magic, happy New Year *~<:o)╔══════════╗║ ?column? ║╠══════════╣║        1 ║╚══════════╝(1 row)                List of relations╔════════╤════════════════════╤══════╤══════════╗║ Schema │        Name        │ Type │  Owner   ║╠════════╪════════════════════╪══════╪══════════╣║ public │ dummy              │ view │ postgres ║║ public │ pg_stat_statements │ view │ nd       ║╚════════╧════════════════════╧══════╧══════════╝