Rails 3.2, FATAL : 사용자에 대한 피어 인증 실패 (PG :: Error)
Ubuntu 11.10 및 RubyMine에서 개발을 진행 중입니다.
다음은 database.yml에 대한 개발 설정입니다. RubyMine이 나를 위해 만든 것입니다.
development:
adapter: postgresql
encoding: unicode
database: mydb_development
pool: 5
username: myuser
password:
앱을 실행하려고 할 때 아래 에이 오류가 발생하지만 아직 '프로젝트'사용자를 만들지 않은 것 같습니다. 그러나 어떻게 사용자를 만들고 postgres에 데이터베이스를 부여 할 수 있습니까? 이것이 문제라면 우분투 에서이 작업을 위해 권장하는 도구는 무엇입니까? 이것이 문제가되지 않으면 조언을 구하십시오.
Exiting
/home/sam/.rvm/gems/ruby-1.9.3-p0@project/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/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `new'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1194:in `connect'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:303:in `new_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:313:in `checkout_new_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/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/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:232:in `loop'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/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/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `retrieve_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/model_schema.rb:308:in `clear_cache!'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activerecord-3.2.3/lib/active_record/railtie.rb:91:in `block (2 levels) in <class:Railtie>'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:418:in `_run__757346023__prepare__404863399__callbacks'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/actionpack-3.2.3/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/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/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/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/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/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/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands/server.rb:46:in `app'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
from /home/sam/.rvm/gems/ruby-1.9.3-p0@project/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
서버에 postresql을 설치 한 경우 host : localhost to database.yml을 호스트로 지정하십시오. 보통 pool이라고하는 곳에 버리십시오. 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.
Ubuntu 13.10 의 개발 환경 에서 rails 앱을 postgres와 함께 사용하는 가장 확실한 방법 입니다.
1) Gemfile에서 postgres YAML 및 'pg'gem을 사용하여 rails 앱을 만듭니다.
$ rails new my_application -d postgresql
2) CRUD 기능을 제공하십시오. postgres가 작동하는지 확인하는 경우 비계를 만듭니다.
$ rails g scaffold cats name:string age:integer colour:string
3) 현재 옵션 호스트 매개 변수를 포함하지 않는 YAML를 생성한다. 나는 이것이 필요하다는 것을 알았다. 개발 섹션을 편집하고 다음 매개 변수를 작성하십시오.rails 4.0.1
-d postgresql
encoding: UTF-8
host: localhost
database: my_application_development
username: thisismynewusername
password: thisismynewpassword
이 database
매개 변수는 아직 종료되지 않은 데이터베이스에 대한 것이고 username
and password
는 존재하지 않는 역할의 자격 증명입니다. 우리는 나중에 그것들을 만들 것입니다!
이 방법 config/database.yml
은 다음과 같습니다 (복사 붙여 넣기에 부끄러움이 없습니다 : 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
Now I've only installed postgres once, so I may be wrong here, but I think postgres automatically creates an administration role with the same credentials as the user you installed postgres as.
4b) So this means you need to change to the user that installed postgres to use the psql command and start the shell:
$ sudo su postgres
And then run
$ psql
5) You'll know you're in the postgres shell because your terminal will look like this:
$ psql
psql (9.1.10)
Type "help" for help.
postgres=#
6) Using the postgresql syntax, let's create the user we specified in config/database.yml
's development section:
postgres=# CREATE ROLE thisismynewusername WITH LOGIN PASSWORD 'thisismynewpassword';
Now, there's some subtleties here so let's go over them.
- The role's username, thisismynewusername, does not have quotes of any kind around it
- Specify the keyword LOGIN after the WITH. If you don't, the role will still be created, but it won't be able to log in to the database!
- The role's password, thisismynewpassword, needs to be in single quotes. Not double quotes.
- Add a semi colon on the end ;)
You should see this in your terminal:
postgres=#
CREATE ROLE
postgres=#
That means, "ROLE CREATED", but postgres' alerts seem to adopt the same imperative conventions of git hub.
7) Now, still in the postgres shell, we need to create the database with the name we set in the YAML. Make the user we created in step 6 its owner:
postgres=# CREATE DATABASE my_application_development OWNER thisismynewusername;
You'll know if you were successful because you'll get the output:
CREATE DATABASE
8) Quit the postgres shell:
\q
9) Now the moment of truth:
$ RAILS_ENV=development rake db:migrate
If you get this:
== CreateCats: migrating =================================================
-- create_table(:cats)
-> 0.0028s
== CreateCats: migrated (0.0028s) ========================================
Congratulations, postgres is working perfectly with your app.
9a) On my local machine, I kept getting a permission error. I can't remember it exactly, but it was an error along the lines of
Can't access the files. Change permissions to 666.
Though I'd advise thinking very carefully about recursively setting write privaledges on a production machine, locally, I gave my whole app read write privileges like this:
9b) Climb up one directory level:
$ cd ..
9c) Set the permissions of the my_application directory and all its contents to 666:
$ chmod -R 0666 my_application
9d) And run the migration again:
$ RAILS_ENV=development rake db:migrate
== CreateCats: migrating =================================================
-- create_table(:cats)
-> 0.0028s
== CreateCats: migrated (0.0028s) ========================================
Some tips and tricks if you muck up
Try these before restarting all of these steps:
The mynewusername user doesn't have privileges to CRUD to the my_app_development
database? Drop the database and create it again with mynewusername as the owner:
1) Start the postgres shell:
$ psql
2) Drop the my_app_development
database. Be careful! Drop means utterly delete!
postgres=# DROP DATABASE my_app_development;
3) Recreate another my_app_development
and make mynewusername the owner:
postgres=# CREATE DATABASE my_application_development OWNER mynewusername;
4) Quit the shell:
postgres=# \q
The mynewusername
user can't log into the database? Think you wrote the wrong password in the YAML and can't quite remember the password you entered using the postgres shell? Simply alter the role with the YAML password:
1) Open up your YAML, and copy the password to your clipboard:
development:
adapter: postgresql
pool: 5
# these are our new parameters
encoding: UTF-8
database: my_application_development
host: localhost
username: thisismynewusername
password: musthavebeenverydrunkwheniwrotethis
2) Start the postgres shell:
$ psql
3) Update mynewusername
's password. Paste in the password, and remember to put single quotes around it:
postgres=# ALTER ROLE mynewusername PASSWORD `musthavebeenverydrunkwheniwrotethis`;
4) Quit the shell:
postgres=# \q
Trying to connect to localhost via a database viewer such as Dbeaver, and don't know what your postgres user's password is? Change it like this:
1) Run passwd
as a superuser:
$ sudo passwd postgres
2) Enter your accounts password for sudo
(nothing to do with postgres):
[sudo] password for starkers: myaccountpassword
3) Create the postgres account's new passwod:
Enter new UNIX password: databasesarefun
Retype new UNIX password: databasesarefun
passwd: password updated successfully
Getting this error message?:
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) You need to give your user the ability to create databases. From the psql shell:
ALTER ROLE thisismynewusername WITH CREATEDB
For permanent solution:
The problem is with your pg_hba. This line:
local all postgres peer
Should be
local all postgres md5
Then restart your postgresql server after changing this file.
If you're on Linux, command would be
sudo service postgresql restart
I was facing same problem on Ubuntu machine so I removed this error by following some steps. Switch to postgres user
$ sudo su - postgres
it will ask for password and by default password is postgres
After switch the user to postgres, open psql console
$ psql
so check the version of postgres if multiple versions are available
psql=# select VERSION();
PostgreSQL 9.1.13 on x86_64-unk.... # so version is 9.1
Now Open postgres user
vim /etc/postgresql/9.1/main/pg_hba.conf
9.1
is version return form upper command
and replace
local all postgres peer
to
local all postgres md5
Restart the service
sudo service postgresql restart
I write steps on my blog also
http://tarungarg402.blogspot.in/2014/10/set-up-postgresql-on-ubuntu.html
You can go to your /var/lib/pgsql/data/pg_hba.conf file and add trust in place of Ident It worked for me.
local all all trust
host all 127.0.0.1/32 trust
For further details refer to this issue Ident authentication failed for user
Adding host: localhost
was the magic for me
development:
adapter: postgresql
database: database_name_here
host: localhost
username: user_name_here
If you get that error message (Peer authentication failed for user (PG::Error)
) when running unit tests, make sure the test database exists.
'IT story' 카테고리의 다른 글
Spark SQL의 DataFrame에서 열 유형을 변경하는 방법은 무엇입니까? (0) | 2020.06.23 |
---|---|
C #의 배열에서 요소를 삭제하는 방법 (0) | 2020.06.23 |
UICollectionViewCell의 콘센트가 왜없는가요? (0) | 2020.06.23 |
URL에서 매개 변수 검색 (0) | 2020.06.23 |
IIS Express 웹 서버를 시작할 수 없습니다. URL을 등록하지 못했습니다. 액세스가 거부되었습니다. (0) | 2020.06.23 |