使用 Kubectl + exec + PSQL 执行多行查询

Execute multiline query with Kubectl + exec + PSQL

我正在尝试 运行 使用 Kubectl 进行查询,如下所示:

kubectl -n employeesns exec -ti employeedpoddb-0 -- psql -d db_people -U postgres 
-c 'create extension if not exists dblink;' 
-c 'SELECT dbemployees."empId" , dbemployees."createdAt" , dbemployees."updatedAt"
from "users" as "dbemployees"
WHERE dbemployees."empId" not in (
    SELECT "empId"
    FROM   dblink('dbname=peopledb','SELECT "empId"  FROM employees')
    AS     dbpeople("empId" varchar)
)'

不过我得到

ERROR:  syntax error at or near "SELECT"
LINE 1: ...SELECT "empId" FROM   dblink(dbname=peopledb,SELECT
                                                                 ^
command terminated with exit code 1

我们如何使用 Kubectl 执行多行 SQL 查询?

是因为你内心的'没有被转义;你会在本地看到同样的东西

$ echo 'hello 'world' from shell'

你只需要转义那些内引号,或者将外引号更改为 " 然后根据你的需要转义这些用法

-c 'SELECT dbemployees."empId" , dbemployees."createdAt" , dbemployees."updatedAt"
from "users" as "dbemployees"
WHERE dbemployees."empId" not in (
    SELECT "empId"
    FROM   dblink('\''dbname=peopledb'\'','\''SELECT "empId"  FROM employees'\'')
    AS     dbpeople("empId" varchar)
)'