我无法弄清楚为什么我无法使用 Node.js
登录Postgresuser: 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:postgres@192.168.99.100:5432/test_db";
var client = new pg.Client(conString);
client.connect();
我从Docker容器运行Postgres。
我启动了另一个容器,可以使用以下命令
成功访问数据库psql -U postgres -d test_db -h 192.168.99.100 -W
此外,我可以使用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 DB。
然后我想也许这就是造成连接问题的所有因素 - 操作系统用户名。标准的Postgres Docker镜像具有 root 作为标准用户名。切换到 root 后,我能够毫无问题地登录。
这里重现的步骤:
获取标准的Postgres Docker镜像
docker pull postgres
使用以下环境变量启动Docker容器:
docker run --name pg -p 5432:5432 -e POSTGRES_PASSWORD=passwrd -e POSTGRES_USER=root -e POSTGRES_DB=dse -d postgres
启动命令行界面并测试连接。这使用了docker主机的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:passwrd@192.168.99.100/dse";
var client = new pg.Client(conString);
client.connect();
console.log(client.connectionParameters);
process.exit(1);
运行应用
node connect.js
PS:我没有编辑这些文件pg_hba.conf
,postgresql.conf
。
我希望这会帮助其他人解决同样的问题。
答案 1 :(得分: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
)