Sequelize与Postgres的连接在ubuntu 18.04上被拒绝
我从win10 PC开发中将自己的nodejs 10.16.3应用程序部署到ubuntu 18.04。在ubuntu上使用pm2 start /ebs/myapp/index.js
启动应用程序后,出现关于sequelize(5.19.1)数据库连接的错误:
$cat index-error.log
Unable to connect to the database: { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5433
at connection.connect.err (/ebs/myapp/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:170:24)
at Connection.connectingErrorHandler (/ebs/myapp/node_modules/pg/lib/client.js:174:14)
at Connection.emit (events.js:198:13)
at Socket.reportStreamError (/ebs/myapp/node_modules/pg/lib/connection.js:72:10)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'SequelizeConnectionRefusedError',
parent:
{ Error: connect ECONNREFUSED 127.0.0.1:5433
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5433 },
original:
{ Error: connect ECONNREFUSED 127.0.0.1:5433
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5433 } }
postgres(11.5)在同一主机上运行,我可以通过命令行成功访问它。pm2日志文件还指示nodejs应用正在监听port 3000
。此应用程序前端是nginx反向代理,可能与此错误无关。
这里是数据库连接字符串:
const Sql = require("sequelize");
const db = new Sql('mydb', 'postgres', `${process.env.DB_PASSWORD}`, {
host: 'localhost',
dialect: 'postgres',
port:5433,
} );
为什么拒绝sequelize连接?
更新:PostgreSQL日志:
$ cat postgresql-11-main.log
2019-10-07 20:24:27.712 UTC [665] postgres@emps ERROR: syntax error at or near "psql" at character 1
2019-10-07 20:24:27.712 UTC [665] postgres@emps STATEMENT: psql
;
ubuntu@:/var/log/postgresql$ cat postgresql-11-main.log.1
2019-10-04 09:10:49.214 UTC [22809] LOG: received fast shutdown request
2019-10-04 09:10:49.221 UTC [22809] LOG: aborting any active transactions
2019-10-04 09:10:49.292 UTC [22809] LOG: background worker "logical replication launcher" (PID 22816) exited with exit code 1
2019-10-04 09:10:49.298 UTC [22811] LOG: shutting down
2019-10-04 09:10:49.435 UTC [22809] LOG: database system is shut down
2019-10-04 09:11:11.561 UTC [1084] LOG: listening on IPv4 address "127.0.0.1", port 5432
2019-10-04 09:11:11.567 UTC [1084] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-04 09:11:11.628 UTC [1096] LOG: database system was shut down at 2019-10-04 09:10:49 UTC
2019-10-04 09:11:11.648 UTC [1084] LOG: database system is ready to accept connections
2019-10-04 09:11:12.196 UTC [1134] [unknown]@[unknown] LOG: incomplete startup packet
2019-10-04 09:11:17.827 UTC [1282] [unknown]@[unknown] LOG: incomplete startup packet
2019-10-04 09:13:42.106 UTC [1084] LOG: received fast shutdown request
2019-10-04 09:13:42.110 UTC [1084] LOG: aborting any active transactions
2019-10-04 09:13:42.114 UTC [1084] LOG: background worker "logical replication launcher" (PID 1102) exited with exit code 1
2019-10-04 09:13:42.115 UTC [1097] LOG: shutting down
2019-10-04 09:13:42.138 UTC [1084] LOG: database system is shut down
2019-10-04 09:13:46.533 UTC [3040] LOG: listening on IPv4 address "127.0.0.1", port 5432
2019-10-04 09:13:46.535 UTC [3040] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-04 09:13:46.566 UTC [3041] LOG: database system was shut down at 2019-10-04 09:13:42 UTC
2019-10-04 09:13:46.577 UTC [3040] LOG: database system is ready to accept connections
2019-10-04 09:13:47.110 UTC [3048] [unknown]@[unknown] LOG: incomplete startup packet
2019-10-04 09:13:52.713 UTC [3082] [unknown]@[unknown] LOG: incomplete startup packet
回答如下:您的PostgreSQL服务器的日志说它是在默认端口5432
上启动的。但是在您的应用中,您尝试连接到5433
。
改为尝试此连接:
const Sql = require("sequelize");
const db = new Sql('mydb', 'postgres', `${process.env.DB_PASSWORD}`, {
host: 'localhost',
dialect: 'postgres',
port: 5432, // instead of 5433
} );