Home > Net >  How to correctly escape characters in command-line PostgreSQL query?
How to correctly escape characters in command-line PostgreSQL query?

Time:11-22

[servername]# su postgres -c 'cd ~; psql -c "SELECT * FROM pg_catalog.pg_user WHERE usename LIKE name;"'
ERROR:  column "name" does not exist
LINE 1: SELECT * FROM pg_catalog.pg_user WHERE usename LIKE name;
                                                            ^

Tried vary ways but no one of them return the correct response. May be there's a prettier way to do command-line queries?

CodePudding user response:

/usr/local/pgsql15/bin/psql  -c "SELECT * FROM pg_catalog.pg_user WHERE usename like 'admin'"

make it two line.

/usr/local/pgsql15/bin/psql test15  -c  \
"SELECT * FROM pg_catalog.pg_user WHERE usename like 'admin'"

or

/usr/local/pgsql15/bin/psql test15  <<<"SELECT * FROM pg_catalog.pg_user WHERE usename like 'admin'"

or

echo "SELECT * FROM pg_catalog.pg_user WHERE usename like 'admin'" | /usr/local/pgsql15/bin/psql test15

percent sign still works.

/usr/local/pgsql15/bin/psql test15  -c  \
"SELECT * FROM pg_catalog.pg_user WHERE usename like '           
  • Related