2015-06-30 41 views
1

如何將變量傳遞到此postgresql代碼中?我知道它適用於前2行和那些變量,但然後錯誤進入DO聲明..postgresql - 嘗試將命令行變量傳遞給sql腳本的錯誤

我使用:p5作爲我的參數。一旦它進入了DO身體的時候,我得到這個錯誤

psql:Test.sql:18: ERROR: syntax error at or near ":" 
LINE 9:  CREATE ROLE foo_readonly LOGIN PASSWORD :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$ 

我收到此錯誤信息。

我打電話就這像這樣一個shell腳本:

"$PSQL" -h $HOST_NM    \ 
     -p $PORT    \ 
     -U foo     \ 
     -v v3=$USER    \ 
     -v p3="'$USER_PWD'"  \ 
     -v p4="'$1'"   \ 
     -v p5="'$READONLY_PWD'" \ 
     -a      \ 
     -f Test.sql postgres | tee >> $LOG 

是否有另一種方式做到這一點?

+1

嘗試登錄密碼 ':P5';相反?.. –

+0

謝謝!這解決了問題! – noober

回答

1

密碼應在報價:

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$