2012-04-03 44 views
124

我正在我的開發在Ubuntu 11.10和RubyMine的的Rails 3.2,FATAL:對等身份驗證失敗,用戶(PG ::錯誤)

這裏是我的database.yml的開發設置:其中的RubyMine爲我創建

development: 
    adapter: postgresql 
    encoding: unicode 
    database: mydb_development 
    pool: 5 
    username: myuser 
    password: 

當我嘗試運行應用程序,我得到下面這個錯誤,似乎我沒有創建一個「項目」用戶還沒有,但是,我怎麼可以創建一個用戶,並授予其數據庫postgres?如果這是問題,那麼在Ubuntu中推薦的工具是什麼?如果這不是問題,那麼請諮詢。

Exiting 
/home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `initialize': FATAL: Peer authentication failed for user "project" (PG::Error) 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `new' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `connect' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:303:in `new_connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:313:in `checkout_new_connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:237:in `block (2 levels) in checkout' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `loop' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `block in checkout' 
    from /home/sam/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `retrieve_connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/model_schema.rb:308:in `clear_cache!' 
    from /home/sam/.rvm/gems/[email protected]/gems/activerecord-3.2.3/lib/active_record/railtie.rb:91:in `block (2 levels) in <class:Railtie>' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:418:in `_run__757346023__prepare__404863399__callbacks' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    from /home/sam/.rvm/gems/[email protected]/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!' 
    from /home/sam/.rvm/gems/[email protected]/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `block in run_initializers' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing' 
    from /home/sam/RubymineProjects/project/config/environment.rb:5:in `<top (required)>' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency' 
    from /home/sam/.rvm/gems/[email protected]/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require' 
    from /home/sam/RubymineProjects/project/config.ru:4:in `block in <main>' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize' 
    from /home/sam/RubymineProjects/project/config.ru:1:in `new' 
    from /home/sam/RubymineProjects/project/config.ru:1:in `<main>' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/server.rb:200:in `app' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands/server.rb:46:in `app' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app' 
    from /home/sam/.rvm/gems/[email protected]/gems/rack-1.4.1/lib/rack/server.rb:252:in `start' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap' 
    from /home/sam/.rvm/gems/[email protected]/gems/railties-3.2.3/lib/rails/commands.rb:50:in `<top (required)>' 
    from /home/sam/RubymineProjects/project/script/rails:6:in `require' 
    from /home/sam/RubymineProjects/project/script/rails:6:in `<top (required)>' 
    from -e:1:in `load' 
    from -e:1:in `<main>' 

Process finished with exit code 1 
+0

我想我發現了一些東西在這裏http://xtremekforever.blogspot.com/2011/05/setup-rails-project-with-postgresql- on.html – simo 2012-04-03 07:14:04

回答

269

如果安裝在服務器上postresql則只是主持人:localhost來的database.yml,我通常在那裏說泳池扔在身邊:5。否則,如果它不是localhost明確告訴應用程序在哪裏可以找到它的數據庫。

development: 
    adapter: postgresql 
    encoding: unicode 
    database: kickrstack_development 
    host: localhost 
    pool: 5 
    username: kickrstack 
    password: secret 

通過創建數據庫並將所有權分配給應用程序的用戶來建立連接,確保您的用戶憑據設置正確。 PostgreSQL裏的9運行時創建一個新用戶:

sudo -u postgres psql 

設置PostgreSQL用戶密碼,如果你還沒有,它只是反斜槓密碼。

postgres=# \password 

創建一個新的用戶名和密碼以及用戶的新數據庫:

您確認創建數據庫,用戶,密碼和設置這些權限後
postgres=# create user "guy_on_stackoverflow" with password 'keepitonthedl'; 
postgres=# create database "dcaclab_development" owner "guy_on_stackoverflow"; 

現在更新您的database.yml文件。不要忘記主機:localhost。

+2

如果您仍然無法登錄並通過本地主機完成連接,然後通過終端返回到postgresql並應用這些8.2約定來建立連接用戶的權限,語法爲GRANT ALL DATABASE [dbname] to [ usrname]。此語法在psql 9+上的工作原理相同,但您可能只能切換用戶擁有的數據庫的權限,然後才能授予所有數據庫:http://www.postgresql.org/docs/8.2/static/sql-grant。 html – 2012-05-12 19:55:16

+28

我的配置中缺少'host:localhost'和'pool:5'。添加它們後,錯誤消失。 – 2012-08-30 06:49:55

+17

對我來說'host:localhost'丟失了。我補充說,一切工作。這是在Ubuntu 13.04 – Jesse 2013-08-02 22:19:43

49

這是最簡單的方式讓您的Rails應用程序在Ubuntu 13.10與Postgres的工作在開發環境

1)創建的Postgres YAML和 'PG' 寶石在Gemfile中Rails應用程序:

$ rails new my_application -d postgresql 

2)給它一些CRUD功能。如果你只是看是否Postgres的工作,創建腳手架:

$ rails g scaffold cats name:string age:integer colour:string 

3)由於rails 4.0.1-d postgresql選項生成一個YAML不包括主機參數。 我發現我需要這個。編輯發展部,並創建以下參數:

encoding: UTF-8 
host: localhost 
database: my_application_development 
username: thisismynewusername 
password: thisismynewpassword 

注意database參數是一個還不退出數據庫和usernamepassword是不存在任何作用的憑據。我們將在稍後創建它們!

這是怎麼config/database.yml應該(在copypasting沒有羞恥:d):

development: 
    adapter: postgresql 
    pool: 5 
    # these are our new parameters 
    encoding: UTF-8 
    database: my_application_development 
    host: localhost 
    username: thisismynewusername 
    password: thisismynewpassword 

test: 
    # this won't work 
    adapter: postgresql 
    encoding: unicode 
    database: my_application_test 
    pool: 5 
    username: my_application 
    password: 

production: 
    # this won't work 
    adapter: postgresql 
    encoding: unicode 
    database: my_application_production 
    pool: 5 
    username: my_application 
    password: 

4)啓動Postgres的外殼用這個命令:

$ psql 

4A),你可能會得到這個錯誤如果您的當前用戶(如您的計算機用戶)沒有相應的管理postgres角色。

psql: FATAL: role "your_username" does not exist 

現在,我只安裝的Postgres一次,所以我可能是錯在這裏,但我認爲Postgres自動創建了爲用戶安裝的Postgres作爲同一憑據的管理角色。

4B)因此,這意味着你需要更改爲安裝Postgres一起使用psql的命令,並啓動shell用戶:

$ sudo su postgres 

然後運行

$ psql 

5)你」因爲你的終端看起來是這樣的:

$ psql 
psql (9.1.10) 
Type "help" for help. 

postgres=# 

6)使用PostgreSQL的語法,讓我們創建我們config/database.yml發展部分指定的用戶:

postgres=# CREATE ROLE thisismynewusername WITH LOGIN PASSWORD 'thisismynewpassword'; 

現在,這裏的一些細微之處讓我們去了他們。

  • 角色的用戶名,thisismynewusername周圍有任何形式的報價
  • 指定關鍵字LOGIN WITH的後。如果你不這樣做,角色仍然會被創建,但它不能登錄到數據庫!
  • 該角色的密碼,thisismynewpassword,需要用單引號。 雙雙引號
  • 添加一個分號就結束;)

你應該可以看到這個在你的終端:

postgres=# 
CREATE ROLE 
postgres=# 

這意味着,「角色創造的」,但Postgres的警報似乎採取同樣的Git Hub的命令性約定。

7)現在,仍然在postgres shell中,我們需要用我們在YAML中設置的名稱創建數據庫。讓我們在第6步創建的用戶它的主人:

postgres=# CREATE DATABASE my_application_development OWNER thisismynewusername; 

你會知道,如果你是成功的,因爲你會得到輸出:

CREATE DATABASE 

8)退出Postgres的外殼:

\q 

9)現在對真理的時刻:

$ RAILS_ENV=development rake db:migrate 

如果你得到這樣的:

== CreateCats: migrating ================================================= 
-- create_table(:cats) 
    -> 0.0028s 
== CreateCats: migrated (0.0028s) ======================================== 

恭喜,Postgres的與您的應用程序可以正常使用。

9a)在我的本地機器上,我不斷收到權限錯誤。我不記得它到底,但它是沿

Can't access the files. Change permissions to 666. 

雖然我建議想很仔細約遞歸生產機器上設置寫privaledges行的錯誤,在本地,我把我的整個應用程序讀取寫這樣的特權:

9B)爬上一級目錄:

$ cd .. 

9C)設置my_application之類目錄的權限及其所有內容,以666:

$ chmod -R 0666 my_application 

9D),然後再次運行遷移:如果你弄髒

重新啓動所有的這些步驟之前,請嘗試以下

$ RAILS_ENV=development rake db:migrate 

== CreateCats: migrating ================================================= 
-- create_table(:cats) 
    -> 0.0028s 
== CreateCats: migrated (0.0028s) ======================================== 

的一些技巧:

的mynewusername用戶沒有權限對my_app_development數據庫執行CRUD操作?除去數據庫,並與mynewusername重新創建它的主人:

1)啓動Postgres的外殼:

$ psql 

2)刪除my_app_development數據庫。小心!下降意味着完全刪除!

postgres=# DROP DATABASE my_app_development; 

3)重新創建另一my_app_development並mynewusername店主:

postgres=# CREATE DATABASE my_application_development OWNER mynewusername; 

4)退出外殼:

postgres=# \q 

mynewusername用戶無法登錄到數據庫?認爲你在YAML中輸入了錯誤的密碼,並且無法完全記住你使用postgres shell輸入的密碼?簡單地改變與YAML口令的角色:

1)打開你的YAML和密碼複製到剪貼板:

development: 
     adapter: postgresql 
     pool: 5 
     # these are our new parameters 
     encoding: UTF-8 
     database: my_application_development 
     host: localhost 
     username: thisismynewusername 
     password: musthavebeenverydrunkwheniwrotethis 

2)啓動Postgres的外殼:

$ psql  

3)更新mynewusername的密碼。粘貼密碼,記得把它周圍的單引號:

postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`; 

4)退出外殼:

postgres=# \q 

嘗試連接通過數據庫瀏覽器如Dbeaver爲localhost,和唐不知道你的postgres用戶的密碼是什麼?改變這樣的:

1)運行passwd作爲超級用戶:

$ sudo passwd postgres 

2)sudo輸入您的帳號密碼(無關的Postgres):

[sudo] password for starkers: myaccountpassword 

3)創建postgres帳戶的新密碼:

Enter new UNIX password: databasesarefun 
Retype new UNIX password: databasesarefun 
passwd: password updated successfully 

收到此錯誤信息?:

Run `$ bin/rake db:create db:migrate` to create your database 
$ rake db:create db:migrate 
PG::InsufficientPrivilege: ERROR: permission denied to create database 

4)你需要給你的用戶創建數據庫的能力。從psql的殼:

ALTER ROLE thisismynewusername WITH CREATEDB 
+0

我希望我能給你更多的+1這個輝煌的解決方案,我會更快樂,如果它爲我工作。仍然輝煌的答案。快速的問題,那些在改變角色密碼的反引號?另外,你不需要提前使用反引號嗎? – 2014-02-14 02:46:46

+0

@ MikeH-R謝謝,可惜它不適合你!你確定你的相關YAML有'host:localhost'嗎?這是我真正的困惑。圍繞密碼,你需要單引號。在我的鍵盤上,這是'shift' +'@'。使用psql shell只需要一些參數的單引號。例如,你需要密碼,而不是**角色的名字。老實說,這很容易,但一旦你知道它是細微差別就好了:) – Starkers 2014-02-14 07:36:01

+0

@ MikeH-R我已經調整了我的答案,使事情更清晰。考慮再次破解它! – Starkers 2014-02-14 08:08:56

28

對於永久的解決方案:

問題是與你的pg_hba。這條線:

local all    postgres        peer 

應該

local all    postgres        md5 

然後更改此文件後重新啓動PostgreSQL服務器。

如果你是在Linux上,命令將

sudo service postgresql restart 
0

如果您收到錯誤消息(Peer authentication failed for user (PG::Error))運行單元測試時,確保測試數據庫中存在。

6

我在Ubuntu機器上遇到同樣的問題,所以我通過以下步驟刪除了這個錯誤。 切換到postgres用戶

$ sudo su - postgres

它會詢問密碼,默認密碼爲postgres

切換用戶Postgres的,開放的psql控制檯後

$ psql 

所以檢查版本postgres如果有多個版本可用

psql=# select VERSION(); 

PostgreSQL 9.1.13 on x86_64-unk....   # so version is 9.1 

現在打開postgres user

vim /etc/postgresql/9.1/main/pg_hba.conf

9.1是版本申報表上的命令

並更換

local all    postgres        peer 

local all    postgres        md5 

sudo service postgresql restart

我寫在我的博客步驟也

http://tarungarg402.blogspot.in/2014/10/set-up-postgresql-on-ubuntu.html

+0

你給出了正確的答案,只錯過了一件你不需要檢查版本的東西。你可以在psql後運行這個命令來更改密碼 修改用戶「username」設置密碼'xyz'; – vishu 2015-01-29 13:28:22

+0

請記住,'md5'身份驗證需要您的數據庫用戶擁有一個非空的密碼(在創建Rails應用程序時儘量使懶惰最大化)。 – 2017-10-11 14:03:54

5

你可以去你/var/lib/pgsql/data/pg_hba.conf文件,並在地方訂貨號 它的添加信任爲我工作。

local all all trust 
host all 127.0.0.1/32 trust 

欲知詳情,請參閱這個問題 Ident authentication failed for user

+0

Centos 6.6,試試:'/ var/lib/pgsql/9.2/data/pg_hba.conf' – Patrick 2015-05-26 23:45:45

相關問題