【发布时间】:2020-10-11 01:36:42
【问题描述】:
我正在尝试使用 GraphQL 和 Knex 运行我的 node-express 服务器,并将其连接到 heroku 中的 PostgresQL 数据库。
当我运行 heroku bash CLI 并尝试迁移时,我收到此错误
~ $ npm run migrate
> syncify-server@0.0.0 migrate /app
> knex migrate:latest
Using environment: staging
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! syncify-server@0.0.0 migrate: `knex migrate:latest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the syncify-server@0.0.0 migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /app/.npm/_logs/2020-06-21T00_20_17_455Z-debug.log
它在本地开发中运行良好。我的 knex.js 文件
import dotenv from 'dotenv'
import knex from 'knex'
import mockKnex from 'mock-knex'
dotenv.config()
let knexConnection
if (process.env.NODE_ENV === 'test') {
knexConnection = knex({
client: 'pg',
debug: false,
})
mockKnex.mock(knexConnection)
} else {
knexConnection = knex({
client: 'pg',
connection: {
url: process.env.DATABASE_URL,
type: 'postgres',
charset: 'utf8',
ssl: false
},
})
}
export default knexConnection
和 knexfile.js
require('dotenv').config()
module.exports = {
development: {
client: 'pg',
connection: {
url: process.env.DATABASE_URL,
charset: 'utf8',
},
},
staging: {
client: 'pg',
connection: {
url: process.env.DATABASE_URL,
charset: 'utf8',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
production: {
client: 'pg',
connection: {
url: process.env.DATABASE_URL,
charset: 'utf8',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
},
},
}
我一直试图找出问题所在,但感觉可能不止一个问题。
在配置变量中,DATABASE_URL 作为 heroku psql db URL,NODE_ENV 作为“暂存”以及所有 auth0 设置。
我可以在命令行中使用psql 访问在线数据库。我有正确的表,可以使用 SQL 语句创建和检索数据。
当我将本地服务器配置为使用 heroku psql db 时,我收到错误消息
relation "users" does not exist
如果我尝试数据库中的其他表也是如此。
我尝试将我的 SSL 更改为 true 会引发错误,而将其更改为 false 似乎不会造成任何损害。 (我尝试了很多其他的东西)
如果我点击在线 heroku 服务器,它只会抛出一个没有任何详细信息的通用错误。
源码here
【问题讨论】:
-
尝试直接访问 API 会引发此错误:``` 错误:连接 ECONNREFUSED 127.0.0.1:80 ``` 这是授权标头设置为允许登录。如果不设置标题,它会正确返回用户需要授权
标签: node.js postgresql heroku psql