2014-03-13 15 views
1

我正在嘗試安裝postgresql併爲它在部署上生成一個隨機密碼,僅用於測試。事情是這樣的:在bash中爲postgresql生成隨機密碼

$DBPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) 
sudo -u postgres -H createuser --no-createrole --no-superuser --no-createdb $NAME 
sudo -u postgres -H createdb -O $PROJECT $PROJECT 
sudo -u postgres -H psql -c "alter user $PROJECT with password '$DBPASS'" 

我回來的錯誤是這樣的:

=b3wDxlSbUymho0CmOZ4TgPylLCanKdgJ: command not found /usr/lib/postgresql/9.1/bin/createdb: option requires an argument -- 'O' Try "createdb --help" for more information.
ERROR: syntax error at or near "with password"
LINE 1: alter user with password ''

任何人都可以請解釋爲什麼會這樣?

回答

3

你的第一線需要改變(變量賦值不應該有一個美元符號):

DBPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) 

錯誤也表明,$PROJECT沒有被設置 - 你得到了什麼,如果你以後添加echo "$PROJECT - $DBPASS"第一行?

+0

謝謝。第二個錯誤是因爲我沒有設置$項目 – Jimmy