无法访问 postgres 文件 pg_hba.conf

can't access postgres file pg_hba.conf

提问人:Jakxna360 提问时间:12/2/2012 最后编辑:TarHaldaJakxna360 更新时间:10/10/2022 访问量:45631

问:

当我运行db:create时出现此错误:

FATAL:  Peer authentication failed for user "wandrr"
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
/home/jack/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
/home/jack/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
/home/jack/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
....Edit out 40 more of these warnings...
/home/jack/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
/home/jack/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"wandrr_test", "pool"=>5, "username"=>"wandrr", "password"=>nil}

我发现我必须编辑 pg_hba.conf。问题是它打不开。它一直告诉我我没有使用这个文件的权限。那么我如何获得许可呢?我正在运行 ubuntu 12.10、rails 1.9.3 和 postgres 9.1。

PostgreSQL Ubuntu

评论

1赞 Trent Earl 12/2/2012
您是否尝试过使用 sudo: 进行编辑?sudo vi pg_hba.conf
1赞 Jakxna360 12/2/2012
叶。。。什么都没有显示。我以为它应该充满文字?
0赞 Trent Earl 12/2/2012
我的也是空的。它可能是别的东西。你能从命令行登录吗?
0赞 Jakxna360 12/2/2012
当我用 gedit 打开它时,我能够进行编辑。也许它只是文本编辑器?
1赞 Craig Ringer 12/2/2012
@JackJenkins 如果您没有指定路径,并且当前目录不是 的位置,那么将尝试在当前目录(可能是您的主文件夹)中打开它,但没有找到它,并开始编辑保存时创建的新文件。您需要指定完整路径,例如从 Ubuntu 软件包安装的 Pg。要获取路径,请使用 .pg_hba.confsudo vi pg_hba.confpg_hba.confsudo vi /etc/postgresql/9.1/main/pg_hba.confSHOW hba_file;psql

答:

31赞 Craig Ringer 12/2/2012 #1

首先记下系统上的位置。如果您不知道,但可以连接,请使用 PgAdmin-III 连接到 Pg 或运行:pg_hba.confpsql

SHOW hba_file;

获取 的位置。它的位置相当标准;在 Ubuntu 上,它将在 例如.在大多数其他发行版上,它将直接或在版本化目录中位于 或 下。pg_hba.conf/etc/postgresql/[major.minor]/main/pg_hba.conf/etc/postgresql/9.1/main/pg_hba.conf/var/lib/pgsql//var/lib/postgresql

若要编辑此文件,必须先指定完整路径或将目录更改为其位置。说:

sudo vi /etc/postgresql/9.1/main/pg_hba.conf

如果您更喜欢更友好的文本编辑器:

sudo nano /etc/postgresql/9.1/main/pg_hba.conf

或者将“nano”替换为您喜欢的编辑器,例如 .gedit

保存更改后,请记住重新加载 PostgreSQL 服务以使其生效。在 Ubuntu 上,您需要:

pg_ctlcluster 9.1 main reload

评论

0赞 Connor Leech 11/12/2013
当我这样做时,除了 pg_hba.conf 之外,我的目录还添加了一个 pg_hba.conf~。编辑 pg_hba.conf 文件的最佳方法是什么?sudo gedit pg_hba.conf
0赞 Nezir 5/11/2019
哦,伙计,在 1:30 小时后寻找解决方案,这对我有帮助!谢谢!
1赞 Valerie Anderson 3/18/2015 #2

遵循 Crag 的建议效果很好。

对于他的最后一步,您必须 sudo 进入数据库超级用户才能重新加载 PostgreSQL 服务:

sudo -u postres pg_ctlcluster 9.1 main reload

这些步骤也适用于 Debian Sid 系统。

1赞 Chris Halcrow 12/16/2019 #3

要编辑,您可以尝试以用户身份进行编辑:pg_hba.confpostgres

  • 切换到用户postgres

    sudo su postgres

  • psql

  • SHOW hba_file;作为 psql 中的查询来获取位置
  • nano *the file location*(不会,因为这将尝试以root用户身份进行编辑操作)sudo nano