0

Having a database with case sensitive name like MyDb,

if I want to use psql to grant all privileges on this database to the user 'user' I can just write:

GRANT ALL PRIVILEGES ON DATABASE "MyDb" TO user;

it works.

But what if I want to use shell scripts?

Running the following command:

su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"MyDb\" TO user;\""

fails with the error like: ERROR: database "mydb" does not exist

2
  • No, its not working! Nov 12, 2015 at 11:40
  • Why use su at all? Why not sudo -u postgres psql -c 'GRANT ALL PRIVILEGES ON DATABASE "MyDb" TO user;'?
    – muru
    Nov 12, 2015 at 13:05

1 Answer 1

0

You could use singlequotes :

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 MyDb      | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 

$ su - postgres -c 'psql -c "grant all privileges on database \"MyDb\" to user;"'  
GRANT

But be careful, as if you're using variables inside the singlequotes, they will not be expanded. In your specific case, it's ok though.

Edit: you also need to escape the doublequotes inside the request, so they are correctly passed to PostGres, in order to indicate that the DB name is case-sensitive.

2
  • It doesn't work! I said the datebase name was case senstive MyDb! Nov 12, 2015 at 10:49
  • I edited the answer to take into account the fact that the DB name is case sensitive.
    – aanc
    Nov 12, 2015 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .