简体   繁体   English

Postgres,Docker和Node.js-密码身份验证失败(连接被拒绝或psql:FATAL:角色“ root”不存在)

[英]Postgres, Docker & Node.js - Password authentication failed (Connection refused or psql: FATAL: role “root” does not exist)

I just can't figure out why I can't login to Postgres using Node.js 我只是不知道为什么我不能使用Node.js登录到Postgres

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

Here are the few lines of code in Node.js that causes the error 这是导致错误的Node.js中的几行代码

error: password authentication failed for user "postgres"

var pg = require('pg');
var conString = 
"postgres://postgres:postgres@192.168.99.100:5432/test_db";

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

I run Postgres from a Docker container. 我从Docker容器运行Postgres。

I fired up another container and can successfully access the DB using the following command 我启动了另一个容器,并可以使用以下命令成功访问数据库

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

Furthermore I can login successfully using pgAdmin 4 此外,我可以使用pgAdmin 4成功登录

pg_hba.conf 的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 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

Any help is appreciated ... 任何帮助表示赞赏...

None of the above helped to connect to the DB via Node.js application 以上都不帮助通过Node.js应用程序连接到数据库

After hours of trial and error I decided to install a Postgres instance on my MAC. 经过数小时的反复试验,我决定在MAC上安装Postgres实例。 At the install Postgres used the login credentials of the OS (in this case my MAC login credentials). 在安装过程中,Postgres使用了操作系统登录凭据 (在本例中为我的MAC登录凭据)。 I was able to login to the Postgres DB with my Node.js application. 我可以使用Node.js应用程序登录到Postgres数据库。

Then I thought perhaps that's all what it is that causes the connection problem - the OS username. 然后我认为可能就是导致连接问题的所有原因-操作系统用户名。 The standard Postgres Docker images has root as the standard username. 标准Postgres Docker映像具有root作为标准用户名。 After switching to root I was able to login without a problem. 切换到root之后,我可以顺利登录。

Here the steps to reproduce: 这里是重现步骤:

  1. Get the standard Postgres Docker image 获取标准的Postgres Docker映像

     docker pull postgres 
  2. Start the Docker container with the following environment variables: 使用以下环境变量启动Docker容器:

     docker run --name pg -p 5432:5432 -e POSTGRES_PASSWORD=passwrd -e POSTGRES_USER=root -e POSTGRES_DB=dse -d postgres 
  3. Start the command line interface and test connectivity. 启动命令行界面并测试连接性。 This uses the IP of the docker host. 这将使用Docker主机的IP。 To get the IP use docker-machine ip . 要获取IP,请使用docker-machine ip The password is passwrd 密码是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=# 

Note: This will also prevent any errors like that: psql: FATAL: role "root" does not exist 注意:这样还可以防止出现以下任何错误: psql: FATAL: role "root" does not exist

  1. Create a Node.js app and a file called connect.js with the following content: 创建一个具有以下内容的Node.js应用程序和一个名为connect.js的文件:

     var pg = require('pg'); var conString = "postgres://root:passwrd@192.168.99.100/dse"; var client = new pg.Client(conString); client.connect(); console.log(client.connectionParameters); process.exit(1); 
  2. Run the app 运行应用

     node connect.js 

PS: I didn't edit neither of these files pg_hba.conf , postgresql.conf . PS:我没有编辑这两个文件pg_hba.confpostgresql.conf

I hope that will help others with the same problem. 我希望这会帮助其他有同样问题的人。

The problem is in pg_hba.conf file: 问题出在pg_hba.conf文件中:

host all all all md5

In the third column (address) you should specify either a domain name or network mask. 在第三列(地址)中,您应该指定域名或网络掩码。 You cannot use all keyword, because it's treated as a domain name, and therefore has no effect. 您不能使用all关键字,因为它被视为域名,因此无效。

To allow all connections: 允许所有连接:

host all all 0.0.0.0/0 md5

Or allow some specific network: 或允许一些特定的网络:

host all all 192.168.0.0/16 md5

Don't forget to reload the config so that the changes take effect ( service postgresql reload or /etc/init.d/postgresql reload ) 不要忘记重新加载配置,以使更改生效( service postgresql reload/etc/init.d/postgresql reload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM