2013-04-02 61 views
0

在碼頭OSX山獅試圖createdb時,那麼以下here,我添加export PATH=/opt/local/lib/postgresql92/bin:$PATH.bash_profile說明我得到了著名密碼問題 - PostgreSQL的上OSX山獅

psql: could not connect to server: No such file or directory 
    Is the server running locally and accepting 
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? 

錯誤現在如果我輸入which psql我得到/opt/local/lib/postgresql92/bin/psql(以前我得到/usr/bin/psql)。

但是,如果我嘗試createdb database現在,我得到提示,因爲我不知道密碼(這不是我的默認的用戶密碼),如果我進錯了兩次,我得到:

createdb: could not connect to database template1: 
FATAL: password authentication failed for user "username" 

其中username是我在終端中的默認用戶名。我想要做的是創建一個PostgreSQL數據庫,我可以從一個生活在virtualenv中的Django項目訪問。我正在使用PostgreSQL 9.2。我究竟做錯了什麼?

回答

2

createdb必須在引擎蓋下連接到PostgreSQL以執行將創建新數據庫的SQL命令。默認情況下,它使用你的操作系統用戶名。根據錯誤信息,它發生錯誤,或者是因爲該用戶不是以數據庫用戶的身份存在(最可信的),或者是因爲他有一個您不記得的密碼。

爲了解決這個問題,假設你使用的macports包,它似乎是合理給出的安裝路徑,你可能會遇到:

sudo su postgres -c '/opt/local/lib/postgresql92/bin/createdb dbname' 

新的數據庫將被Postgres用戶,這並不總是被擁有理想。 一個更明智的選擇是創建與您的用戶名普通的數據庫用戶,並使其成爲數據庫的所有者:

sudo su postgres -c '/opt/local/lib/postgresql92/bin/createuser username' 
sudo su postgres -c '/opt/local/lib/postgresql92/bin/createdb -O username dbname' 

編輯: 的createuser命令提示這些問題(從手冊頁):

 $ createuser joe 
     Shall the new role be a superuser? (y/n) n 
     Shall the new role be allowed to create databases? (y/n) n 
     Shall the new role be allowed to create more new roles? (y/n) n 

通過回答是最後兩個,新用戶將有足夠的權限,以便它可以隨後使用例如通過postgressudo,例如這將工作:

createuser -U username newuser 
createdb -U username newdatabase 
+0

謝謝你的答案,丹尼爾!有沒有辦法做到這一切,而不需要每次都使用'sudo'?就像我用我的用戶名創建一個新用戶一樣,我可以切換到那個用戶並以這種方式創建數據庫嗎?或者會有目錄權限等問題? – GChorn

+0

另外,這些命令在哪裏創建數據庫?我似乎無法在我的電腦上找到它。 – GChorn

+1

@GChorn:看看我的關於createuser的編輯。至於數據庫,PG的datadir中每個數據庫都有一個目錄,但它完全由postgres管理,用戶不能對這些目錄和文件進行任何操作。 –