MakoBuk
MakoBuk

Reputation: 464

Postgres - update command from CLI

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?

Upvotes: 2

Views: 12098

Answers (3)

MakoBuk
MakoBuk

Reputation: 464

How a_horse_with_no_name said.

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

Upvotes: 6

wildplasser
wildplasser

Reputation: 44250

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

Upvotes: 0

edenzik
edenzik

Reputation: 146

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.

Upvotes: 0

Related Questions