在psql命令中为结构脚本转义引号

Mas*_*ran 3 python bash shell fabric psql

这个问题的基础上,我试图用fabric命令删除postgresql数据库中的所有表.我正在尝试运行的bash命令是

#!/bin/bash
TABLES=`psql $PGDB -t --command "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public'"`

echo Dropping tables:${TABLES}
psql $PGDB --command "DROP TABLE IF EXISTS ${TABLES} CASCADE"
Run Code Online (Sandbox Code Playgroud)

在我的fab脚本里面变成:

def delete_tables():
    the_command = "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public'"
    run("TABLES=`psql -U db_user -d db_name $PGDB -t --command %s`" % the_command)
Run Code Online (Sandbox Code Playgroud)

但错误是,Peer authentication failed for user "string_agg".这似乎表明该命令不被视为""之间的命令,而是一个长的单个字符串......

我已经试过转换: ''\'' ,但没有运气.欢迎任何建议.

jd.*_*jd. 6

使用pipes.quote()引用的东西,去外壳.

import pipes
def delete_tables():
    the_command = "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public'"
    run("TABLES=`psql -U db_user -d db_name $PGDB -t --command %s`" % pipes.quote(the_command))
Run Code Online (Sandbox Code Playgroud)