簡體   English   中英

PostgreSQL:致命 - 用戶的對等身份驗證失敗 (PG::ConnectionBad)

[英]PostgreSQL: FATAL - Peer authentication failed for user (PG::ConnectionBad)

我正在使用 PostgreSQL,我有一個用戶的密碼與 database.yml 中指定的密碼匹配

postgres=# select * from pg_user
;
  usename   | usesysid | usecreatedb | usesuper | usecatupd | userepl |  passwd  | valuntil | useconfig 
------------+----------+-------------+----------+-----------+---------+----------+----------+-----------
 goodsounds |    16386 | t           | t        | t         | t       | ******** |          | 
 postgres   |       10 | t           | t        | t         | t       | ******** |          | 
(2 rows)

但是當我嘗試通過運行命令創建數據庫時

rails db:create

我收到錯誤

致命:用戶“goodsounds”的對等身份驗證失敗

這是我的 pg_hba.conf:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

以前上面的“信任”是 md5,但我改變了,看看這是否有幫助。

這是我的database.yml:

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On Mac OS X with macports:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
development:
  adapter: postgresql
  encoding: unicode
  database: goodsounds_development
  pool: 5
  username: goodsounds
  password: test

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  host: localhost
  port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # The server defaults to notice.
  #min_messages: warning

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: postgresql
  encoding: unicode
  database: goodsounds_test
  pool: 5
  username: goodsounds
  password: test

production:
  adapter: postgresql
  encoding: unicode
  database: goodsounds_production
  pool: 5
  username: goodsounds
  password: test

“對等身份驗證”意味着它使用 unix 套接字並期望連接的 unix 用戶具有與 postgresql 用戶名相同的 unix 用戶名。

由於您的本地 unix 用戶名是funkdified並且您嘗試通過pg_hba.conf指定peer身份驗證的 unix 域套接字( local )連接以用戶goodsounds身份進行連接,因此 Pg 正確拒絕了您的連接嘗試。

這是使用 unix 套接字時許多安裝的默認行為。

你可以:

  • 通過在數據庫連接設置中指定主機名,通過 TCP/IP 進行連接;
  • 編輯pg_hba.conf以對 unix 套接字( local連接類型)使用md5密碼身份驗證而不是對peer身份驗證,以便 Pg 接受密碼身份驗證; 要么
  • 使用與您的 unix 用戶名相同的 PostgreSQL 用戶名進行連接,並在 PostgreSQL 中創建該用戶(如果尚不存在)。

請參閱pg_hba.conf的文檔以及文檔客戶端身份驗證章節的其余部分。

請注意,對pg_hba.conf更改不會立即生效,您必須重新啟動或至少重新加載 PostgreSQL 以使其重新讀取pg_hba.conf


哦,另外,如果你安裝了多個 PostgreSQL 版本,你可能有一個版本的 libpq 和另一個版本的服務器。 在這種情況下,請確保 libpq 默認連接到的 unix 套接字的位置與服務器的unix_socket_directories相同,或者在連接字符串中使用(例如) host=/tmp覆蓋它。

我在 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

如果“對等身份驗證”不起作用,請嘗試 md5 身份驗證。

要指定主機嘗試這樣的事情:

 psql -d <dbname> -U <username> -h <hostname>

或這個:

 psql -d <dbname> -U <username> -h <hostname> -W

編輯 /etc/postgresql/9.3/main/pg_hba.conf

# "local" is for Unix domain socket connections only
    local   all             all                                     peer

更改為下面的行,它對我有用

# "local" is for Unix domain socket connections only
local   all             all                                     md5

這是我解決它的方法

運行以下命令以確認您的 PostgreSQL 版本。

psql --version

導航到服務器上的PostgreSQL配置目錄,該目錄位於/etc/postgresql/10/main 請注意, 10是我在服務器上安裝的PostgreSQL版本。 您的版本可能是9.51112或任何其他版本。

cd ~
cd /etc/postgresql/10/main

導航到/etc/postgresql/10/main目錄后,使用以下命令打開文件pg_hba.conf 此文件控制:允許連接哪些主機、客戶端如何進行身份驗證、它們可以使用哪些 PostgreSQL 用戶名、它們可以訪問哪些數據庫:

sudo nano pg_hba.conf

替換以下行:

# Database administrative login by Unix domain socket
local       all       postgres       peer

與下面的行:

# Database administrative login by Unix domain socket
local       all       postgres       md5

另外,替換下面的以下行:

# "local" is for Unix domain socket connections only
local       all       all            peer

與下面的行:

# "local" is for Unix domain socket connections only
local       all       all            md5

此外,我們可能希望在生產中允許到 PostgreSQL 數據庫的入站連接(以允許來自所有數據庫、所有用戶、使用 md5 方法的所有地址的主機連接)。

為此,請在文件末尾添加以下行,然后保存文件:

# remote connections
host      all      all      all      md5

仍在/etc/postgresql/10/main目錄中,使用以下命令打開並編輯文件postgresql.conf

sudo nano postgresql.conf

# listen_address='127.0.0.1'listen_address='127.0.0.1'# listen_address='localhost'listen_address='localhost'行替換為下面的行,以允許PostgreSQL 數據庫偵聽來自所有地址的連接:

listen_addresses = '*'

保存文件,然后導航到服務器的根目錄:

cd ~

使用以下命令重新啟動或重新加載 PostgreSQL 服務器:

sudo systemctl restart postgresql       # To restart
sudo systemctl reload postgresql        # To reload

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM