PostgreSQL FATAL: Peer authentication failed for user Error 해결하기

rails의 db를 postgresql로 바꾸고 하는중에 이런 에러가 발생했습니다.

root@cherry:/home/cherry/cherry_web# rake db:setup
FATAL:  Peer authentication failed for user "cherry-web"
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/usr/lib/ruby/vendor_ruby/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'

계정도 제대로 생성하고, 패스워드도 잘 줬고 권한도 잘 줬는데 왜그럴까 고민, 구글링 하다보니 해답이 나오네요. 우선 대다수 application의 인증 과정에서 패스워드는 md5로 사용이 되는데, 간혹 postgresql에서 peer로 지정되서 md5 데이터로 로그인이 안될 수 있더군요.

pg_hba.conf 파일을 수정해서 해결이 가능합니다. 쭉 내리다 보면 접근 제어 관련 내용이 있는데 peer를 md5로 변경해줍니다.

# vim /etc/postgresql/9.6/main/pg_hba.conf

그다음 재시작 시켜주면..

# /etc/init.d/postgresql restart
[ ok ] Restarting postgresql (via systemctl): postgresql.service.

잘 됩니다 :)

root@cherry:/home/cherry/cherry_web# rake db:setup
/home/cherry/cherry_web/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/cherry/cherry_web/config/application.rb to limit the frameworks that will be loaded.
root@cherry:/home/cherry/cherry_web#
root@cherry:/home/cherry/cherry_web# rake db:migrate