2011-05-12 171 views
0

我想在linux服務器上創建用戶和數據庫。 我可以用下面的代碼創建用戶:在postgres中創建數據庫時出現錯誤

su - postgres 
# createuser -S -D -R myUser 

但是當我試圖用代碼創建數據庫:

# createdb -U myUser -p 5432 myDatabase 

我獲得以下錯誤:

createdb: could not connect to database postgres: FATAL: Ident authentication failed for user "myUser" 

我新來的Linux,所以我無法弄清楚爲什麼我能夠創建用戶,但創建數據庫時,與Postgres的連接錯誤。對於用戶而言,身份驗證 也是錯誤的。

+0

我認爲這個問題可能更適合於serverfault或superuser或unix.stackexchange。可能你會在那裏得到更好的答案。這個問題與編程沒有太大關係(什麼是stackoverflow的目的)。 – bmk 2011-05-12 08:40:10

回答

2

I am new to linux so I am unable to figure out why I was able to create user but while creating database there is connection error with postgres. And also error for ident authentication for user.

的ident是依賴於當前登錄的用戶的的認證模式。如果你使用了postgres,然後嘗試以另一個用戶身份登錄,ident將會失敗(因爲它不是當前登錄的用戶)。你可以用兩種方法解決這個問題,我傾向於使用後者。

解決:只需確保當前登錄的用戶是與你想登錄到Postgres的用戶:

[email protected]:~$ createuser -S -D -R myUser 
[email protected]:~$ exit 
machine:~# su - myUser 
[email protected]:~$ psql myDatabase 

更好的解決方案:變化的pg_hba.conf(大概位於/etc/postgresql/8.4/main/或類似),並確保將「md5」身份驗證模式添加到選項列表中。這是我對我的發展箱的pg_hba.conf,沒有評論:

[email protected]:~$ sudo cat /etc/postgresql/8.4/main/pg_hba.conf | grep -v "^#" | grep -v "^$" 
local all   postgres       ident 
local all   all        md5 
host all   all   127.0.0.1/32   md5 
host all   all   ::1/128    md5 

這告訴PostgreSQL的是Postgres的可的ident登錄,而所有其他用戶可以登錄使用MD5認證。這樣一來,就可以使用-U切換到PSQL二進制來表示你希望成爲其用戶,所以實際上的工作原理:

[email protected]:~$ psql -U myUser myDatabase. 

這麼說,我傾向於使用Postgres超級用戶創建數據庫。然後,我將新創建的數據庫的權限授予新創建的用戶,因此:

[email protected]:~$ psql template1 
template1=# CREATE USER myUser WITH PASSWORD 'myPassword'; 
CREATE ROLE 
template1=# CREATE DATABASE myDatabase; 
CREATE DATABASE 
template1=# GRANT ALL PRIVILEGES ON DATABASE myDatabase TO myUser; 
GRANT 
template1=# \q 

希望有所幫助。

附錄:一旦你改變了pg_hba.conf,你將不得不重新啓動PostgreSQL服務器,以確保它再次讀取配置。您可以通過發出以下命令這樣做:

[email protected]:~$ /etc/init.d/postgresql-8.4 restart 

腳本可能會因操作系統和安裝的方法被稱爲「PostgreSQL的」而不是「的PostgreSQL-8.4」。

0

見例如:this link

請使用谷歌搜索,你給句話...