postgresql - 尝试将变量从命令行传递到 sql 脚本时出错
postgresql - error trying to pass a variable from command line to a sql script
如何将变量传递到此 postgresql 代码中?我知道它适用于前两行和那些变量,但随后进入 DO
语句时出错..
我使用 :p5
作为我的参数。
CREATE ROLE :v3 LOGIN ENCRYPTED PASSWORD :p3 NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
ALTER ROLE :v3 SET search_path = foo_bar_dev_:v4;
DO
$body$
BEGIN
IF NOT EXISTS (
SELECT *
FROM pg_catalog.pg_user
WHERE usename = 'foo_readonly') THEN
CREATE ROLE foo_readonly LOGIN PASSWORD :p5 ;
END IF;
END
$body$
我收到此错误消息:
psql:Test.sql:18: ERROR: syntax error at or near ":"
LINE 9: CREATE ROLE foo_readonly LOGIN PASSWORD :p5 ;
^
一旦它进入 DO
正文,我就会收到此错误。
我在 shell 脚本中这样称呼它:
"$PSQL" -h $HOST_NM \
-p $PORT \
-U foo \
-v v3=$USER \
-v p3="'$USER_PWD'" \
-v p4="''" \
-v p5="'$READONLY_PWD'" \
-a \
-f Test.sql postgres | tee >> $LOG
还有其他方法吗?
密码应该在引号中:
CREATE ROLE :v3 LOGIN ENCRYPTED PASSWORD :p3 NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
ALTER ROLE :v3 SET search_path = foo_bar_dev_:v4;
DO
$body$
BEGIN
IF NOT EXISTS (
SELECT *
FROM pg_catalog.pg_user
WHERE usename = 'foo_readonly') THEN
CREATE ROLE foo_readonly LOGIN PASSWORD ':p5' ;
END IF;
END
$body$
如何将变量传递到此 postgresql 代码中?我知道它适用于前两行和那些变量,但随后进入 DO
语句时出错..
我使用 :p5
作为我的参数。
CREATE ROLE :v3 LOGIN ENCRYPTED PASSWORD :p3 NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
ALTER ROLE :v3 SET search_path = foo_bar_dev_:v4;
DO
$body$
BEGIN
IF NOT EXISTS (
SELECT *
FROM pg_catalog.pg_user
WHERE usename = 'foo_readonly') THEN
CREATE ROLE foo_readonly LOGIN PASSWORD :p5 ;
END IF;
END
$body$
我收到此错误消息:
psql:Test.sql:18: ERROR: syntax error at or near ":"
LINE 9: CREATE ROLE foo_readonly LOGIN PASSWORD :p5 ;
^
一旦它进入 DO
正文,我就会收到此错误。
我在 shell 脚本中这样称呼它:
"$PSQL" -h $HOST_NM \
-p $PORT \
-U foo \
-v v3=$USER \
-v p3="'$USER_PWD'" \
-v p4="''" \
-v p5="'$READONLY_PWD'" \
-a \
-f Test.sql postgres | tee >> $LOG
还有其他方法吗?
密码应该在引号中:
CREATE ROLE :v3 LOGIN ENCRYPTED PASSWORD :p3 NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
ALTER ROLE :v3 SET search_path = foo_bar_dev_:v4;
DO
$body$
BEGIN
IF NOT EXISTS (
SELECT *
FROM pg_catalog.pg_user
WHERE usename = 'foo_readonly') THEN
CREATE ROLE foo_readonly LOGIN PASSWORD ':p5' ;
END IF;
END
$body$