Postgres - update command from CLI

你说的曾经没有我的故事 提交于 2020-08-08 07:17:09

问题


I need to create script in linux. There is command like:

psql -U postgres -d ticketon -c "UPDATE "user" SET "password" = 'test'"

When I put it to quotation marks, there is mistake. When out of quotation marks, there also is error. I have tried almost everything, but still without success. Do anyone know what is correct syntax?


回答1:


How a_horse_with_no_name said.

psql -U postgres -d dbName -c "UPDATE \"user\" SET \"password\" = 'test'"



回答2:


I was doing something similar recently. What you want to do is preform the logon first (psql -U postgres -d ticketon -c) and then pipe via STDIN the query you wish to preform ("UPDATE "user" SET "password" = 'test'"). In bash this will be the following:

    echo "UPDATE "user" SET "password" = 'test'" | psql -U postgres -d ticketon -c 

The above could work better than just using the -c command for a single command, because postgres does not like dealing with double quotes.




回答3:


No need for excessive quoting if you use an sh here document:

#!/bin/sh
psql -U postgres -d ticketon <<XXX
UPDATE "user"
SET password = 'test'
WHERE username = 'James' -- I think a where clause is needed here ...
   ;
XXX


来源:https://stackoverflow.com/questions/24656217/postgres-update-command-from-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!