使用sequelize-auto从Heroku数据库生成模型时遇pg_hba.conf错误
问题原因
Heroku Postgres强制要求所有外部连接使用SSL加密,你的sequelize-auto命令未启用SSL连接,导致Postgres服务器拒绝请求,抛出no pg_hba.conf entry... no encryption错误。
解决方案
方案1:直接修改sequelize-auto命令添加SSL参数
在命令中加入SSL配置,告诉工具启用加密连接:
sequelize-auto -h (host) -d (database) -u (user) -x (password) -p (port) --dialect postgres --ssl -c "./server/config/config.json" -o "./server/models"
如果仍报错,需适配Heroku证书配置,添加rejectUnauthorized参数:
sequelize-auto -h (host) -d (database) -u (user) -x (password) -p (port) --dialect postgres --additional '{"ssl": {"rejectUnauthorized": false}}' -c "./server/config/config.json" -o "./server/models"
方案2:修正config.json生产环境配置
更新config.json的production字段,匹配Heroku数据库信息并启用SSL:
{ "development": { "username": "root", "password": null, "database": "database_development", "host": "127.0.0.1", "dialect": "postgres" }, "test": { "username": "root", "password": null, "database": "database_test", "host": "127.0.0.1", "dialect": "postgres" }, "production": { "username": "(你的Heroku数据库用户名)", "password": "(你的Heroku数据库密码)", "database": "(你的Heroku数据库名)", "host": "(你的Heroku数据库主机地址)", "dialect": "postgres", "port": (你的Heroku数据库端口), "ssl": true, "dialectOptions": { "ssl": { "rejectUnauthorized": false } } } }
设置环境变量指定生产环境后运行命令:
NODE_ENV=production sequelize-auto -c "./server/config/config.json" -o "./server/models"
(注:此时命令可省略-h/-d/-u/-x/-p,参数已在config.json中配置)
额外提示
- 本地
index.js逻辑无问题,当前报错和该文件无关——sequelize-auto是独立工具,生成模型时不会直接调用它。 - 可通过Heroku CLI获取完整数据库连接信息:运行
heroku pg:credentials:url,复制输出的连接字符串,包含所有必要参数。
内容的提问来源于stack exchange,提问作者sunny tran
相关产品推荐
相关产品推荐

