相关疑难解决方法(0)

是否可以在 psql 中组合“-c”和“-v”?

抽象的

为什么这不起作用:

$ psql -X -h localhost -d mydatabase -U postgres -v myschema=foo -c "SELECT :'myschema';"
ERROR:  syntax error at or near ":"
LINE 1: SELECT :'myschema';
               ^
Run Code Online (Sandbox Code Playgroud)

但这按预期工作:

$ psql -X -h localhost -d mydatabase -U postgres -v myschema=foo

mydatabase=# SELECT :'myschema';
 ?column? 
----------
 foo
(1 row)
Run Code Online (Sandbox Code Playgroud)

是否可以在通话中组合-c和。-vpsql

语境

我正在尝试使用 bash 脚本执行DO这个)PostgreSQL 语句psql -c,我想将其中一个WHERE条件作为psql 变量传递

像这样的东西:

SCHEMA='foo'

! read -d '' sql_query << "EOF"
DO
$func$ …
Run Code Online (Sandbox Code Playgroud)

postgresql bash psql

5
推荐指数
1
解决办法
359
查看次数

从 PL/PGSQL 引用会话变量 (\set var='value')

我可以使用

psql --variable="var='value'" <<<'SELECT :var'
Run Code Online (Sandbox Code Playgroud)

...并在这种情况下将它们称为:var在标准输入上传递给 psql 的 SQL 查询中。

但是,这不适用于使用 PL/PGSQL 的代码:

psql --variable=var="'value'" <<'EOF'
  DO $$
  BEGIN
    SELECT :var;
  END;
  $$
EOF
Run Code Online (Sandbox Code Playgroud)

...产生错误:

ERROR:  syntax error at or near ":"
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

postgresql scope plpgsql psql

3
推荐指数
1
解决办法
3081
查看次数

DO 脚本中的 PSQL 命令行参数

我有一个NewSchemaSafe.sql基于项目目录创建新模式的脚本;它是从 Windows 命令行调用的,如下所示:

for %%a in (.) do set this=%%~na
-- other stuff here
psql -U postgres -d SLSM -e -v v1=%this% -f "NewSchemaSafe.sql"
Run Code Online (Sandbox Code Playgroud)

NewSchemaSafe.sql 如下:

-- NewSchemaSafe.sql
-- NEW SCHEMA SETUP 
--    - checks if schema exists
--    - if yes, renames existing with current monthyear as suffix
-- NOTE: will always delete any schema with the 'rename' name (save_schema)
--       since any schema thus named must have resulted from this script 
--       on this date - …
Run Code Online (Sandbox Code Playgroud)

postgresql command-line plpgsql parameter-passing psql

2
推荐指数
1
解决办法
4212
查看次数