2017-04-21 97 views
1

我只是想不出爲什麼我無法使用登錄到Postgres Node.jsPostgres,Docker&Node.js - 密碼身份驗證失敗(連接被拒絕或psql:FATAL:角色「root」不存在)

user: postgres 
password: postgres 
host: 192.168.99.100 
port: 5432 
database: test_db 

這裏是代碼的Node.js的幾行導致錯誤

error: password authentication failed for user "postgres"

var pg = require('pg'); 
var conString = 
"postgres://postgres:[email protected]:5432/test_db"; 

var client = new pg.Client(conString); 
client.connect(); 

我跑Postgr es來自Docker容器。

我解僱了另一個容器中,可以使用下面的命令

psql -U postgres -d test_db -h 192.168.99.100 -W 

而且我可以用成功登錄成功訪問DB pgAdmin 4

的pg_hba.conf

# TYPE DATABASE  USER   ADDRESS     METHOD 

# "local" is for Unix domain socket connections only 
local all    all          trust 
# 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        trust 
#host replication  postgres  127.0.0.1/32   trust 
#host replication  postgres  ::1/128     trust 

host all all all md5 

postgresql.conf

#------------------------------------------------------------------------------ 
# CONNECTIONS AND AUTHENTICATION 
#------------------------------------------------------------------------------ 

# - Connection Settings - 

listen_addresses = '*'     # comma-separated list of addresses; 
             # defaults to 'localhost'; use '*' for all 
             # (change requires restart) 
#port = 5432       # (change requires restart) 
max_connections = 100     # (change requires restart) 
#superuser_reserved_connections = 3  # (change requires restart) 
#unix_socket_directories = '/var/run/postgresql'  # comma-separated list of directories 
             # (change requires restart) 
#unix_socket_group = ''     # (change requires restart) 
#unix_socket_permissions = 0777   # begin with 0 to use octal notation 
             # (change requires restart) 
#bonjour = off       # advertise server via Bonjour 
             # (change requires restart) 
#bonjour_name = ''      # defaults to the computer name 
             # (change requires restart) 

# - Security and Authentication - 

#authentication_timeout = 1min   # 1s-600s 
#ssl = off        # (change requires restart) 
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers 
            # (change requires restart) 
#ssl_prefer_server_ciphers = on   # (change requires restart) 
#ssl_ecdh_curve = 'prime256v1'   # (change requires restart) 
#ssl_cert_file = 'server.crt'   # (change requires restart) 
#ssl_key_file = 'server.key'   # (change requires restart) 
#ssl_ca_file = ''      # (change requires restart) 
#ssl_crl_file = ''      # (change requires restart) 
#password_encryption = on 
#db_user_namespace = off 
#row_security = on 

# GSSAPI using Kerberos 
#krb_server_keyfile = '' 
#krb_caseins_users = off 

# - TCP Keepalives - 
# see "man 7 tcp" for details 

#tcp_keepalives_idle = 0    # TCP_KEEPIDLE, in seconds; 
             # 0 selects the system default 
#tcp_keepalives_interval = 0   # TCP_KEEPINTVL, in seconds; 
             # 0 selects the system default 
#tcp_keepalives_count = 0    # TCP_KEEPCNT; 
             # 0 selects the system default 

任何幫助表示讚賞...

+0

確保您的密碼很簡單。例如,不能有任何哈希標籤,因爲驅動程序不支持它;) –

回答

2

幫助以上都不是通過Node.js的應用程序連接到數據庫

經過數小時的試驗和錯誤,我決定在我的MAC上安裝一個Postgres實例。在安裝時,Postgres使用了操作系統的登錄憑證(在本例中爲我的MAC登錄憑證)。我可以用我的Node.js應用程序登錄到Postgres數據庫。

然後我想也許這就是它導致連接問題 - 操作系統用戶名。標準Postgres Docker鏡像具有作爲標準用戶名。切換到我能夠登錄沒有問題。重現

下面的步驟:

  1. 得到標準的Postgres泊塢窗圖像

    docker pull postgres 
    
  2. 啓動泊塢容器以下環境變量:

    docker run --name pg -p 5432:5432 -e POSTGRES_PASSWORD=passwrd -e POSTGRES_USER=root -e POSTGRES_DB=dse -d postgres 
    
  3. 開始命令行界面和測試連接。這使用泊塢窗主機的IP。獲得IP使用docker-machine ip。密碼是passwrd

    docker exec -ti pg /bin/bash 
    
    psql -U root -d dse -h 192.168.99.100 -W 
    Password for user root: 
    psql (9.6.2) 
    Type "help" for help. 
    
    dse=# 
    

注:這也將防止任何錯誤這樣的:psql: FATAL: role "root" does not exist

  • 創建的Node.js應用程序和一個文件名爲connect.js,內容如下:

    var pg = require('pg'); 
    
    var conString = "postgres://root:[email protected]/dse"; 
    var client = new pg.Client(conString); 
    client.connect(); 
    console.log(client.connectionParameters); 
    process.exit(1); 
    
  • Run th Ë應用

    node connect.js 
    
  • PS:我沒有修改這兩個文件都不pg_hba.confpostgresql.conf的。

    我希望能幫助其他人解決同樣的問題。

    0

    的問題是在pg_hba.conf文件:

    host all all all md5 
    

    在第三列(地址),你應該指定一個域名或網絡掩碼。您不能使用all關鍵字,因爲它被視爲域名,因此不起作用。

    要允許所有連接:

    host all all 0.0.0.0/0 md5 
    

    ,或者允許一些特定的網絡:

    host all all 192.168.0.0/16 md5 
    

    不要忘記重新加載配置,以使更改生效(service postgresql reload/etc/init.d/postgresql reload

    0

    根據您的Docker network settings(或者您的Node.js應用程序位於同一子網內的其他主機上),則應相應地修改您的pg_hba.conf以獲取客戶端連接。由於您的PostgreSQL用戶至少爲遠程連接設置了密碼驗證,因此我建議您使用md5作爲驗證方法。

    如果碼頭工人在默認bridge網絡設置,子網通常是172.17.0.0/16(在Ubuntu):

    host all    all    172.17.0.0/16   md5 
    

    如果碼頭工人在host網絡設置,子網可能是192.168.99。0/24:

    host all    all    192.168.99.0/24   md5 
    

    要打開所有的遠程主機(是提醒注意安全的啓示):

    host all    all    0.0.0.0/0    md5 
    
    +0

    我做了更改並重新啓動服務器 - 仍然沒有運氣。我可以啓動另一個容器,並使用正在運行的容器'docker run -it -rm test_db psql -U postgres -h 172.17.0.2'的IP,它工作正常。同時,我在我的MAC上本地安裝了一個postgres實例。由於它與我的電腦登錄名和密碼一起安裝,因此我可以連接到節點。它必須與權限有關。 –

    +0

    因爲你的節點。js以明文形式發送驗證數據,請嘗試將auth方法從'md5'切換到'pg_hba'中的'password',重新啓動PostgreSQL並查看是否有效。 –