2

I have a fabfile that I would like to run a command to create a postgres database.

The SQL request is:

CREATE DATABASE "dbname"
WITH ENCODING = 'UTF8'
     LC_COLLATE = 'en_US.UTF8'
     LC_CTYPE = 'en_US.UTF8'
     TEMPLATE = template0 
     OWNER = "dbowner";

I want to run it using:

from fabric.api import run
run("""sudo su postgres --command "psql -c 'REQUEST HERE'" """)

But I couldn't find any way of escaping it the right way.

Do you have any idea of how I could do it?

4

2 回答 2

1

好的,我终于这样做了:

run('sudo su postgres --command \'psql -e -c "CREATE DATABASE peopleask WITH ENCODING = \'"\'"\'UTF8\'"\'"\' LC_COLLATE = \'"\'"\'en_US.UTF8\'"\'"\' LC_CTYPE = \'"\'"\'en_US.UTF8\'"\'"\' TEMPLATE = template0 OWNER = peopleask;"\'')
于 2013-08-20T09:26:24.090 回答
0

使用 a\在双引号字符串中转义双引号,对于单引号字符串中的单引号也是如此。

于 2013-08-20T09:03:58.603 回答