Vapor框架:配置带有SSL的PostgreSQL连接

6

我正在尝试连接我的Heroku PostgreSQL数据库,但出现以下错误:

cannotEstablishConnection("FATAL:  no pg_hba.conf entry for host \"37.167.93.189\", user \"clpnkpyttmdtyq\", database \"d3h6147v73mgtu\", SSL off\n")

我知道Heroku的Postgres数据库需要使用SSL连接,但我不知道如何在我的Droplet对象上配置连接。
这是我的postgresql.json配置文件:
{
    "host": "ec2-54-163-224-108.compute-1.amazonaws.com",
    "user": "clpnkpyttmdtyq",
    "password": "99201aa07c48e18e7bdf210937857b85bee37cd8d8cb904381b1ddff934c7a4f",
    "database": "d3h6147v73mgtu",
    "port": 5432
}

也许有我不知道的 ssl 参数?

如何添加 VaporPostgresSQLProvider

let drop = Droplet()

// Tell the droplet to use our SQL provider service
try drop.addProvider(VaporPostgreSQL.Provider.self)

有什么想法吗?

当我尝试使用我的本地 postgres 数据库时,它可以工作,因为它不需要 SSL 连接。


https://github.com/vapor/postgresql/issues/15 - Vao Tsun
这个还在进行中吗? - AnthonyR
1
不知道 - 只是想知道你使用的是什么,并找到了链接。 - Vao Tsun
我尝试从本地Mac远程访问Heroku的PostgreSQL,但发生了同样的事情。 - Genki
3个回答

2

这让我节省了很多时间- 从2021年3月开始,所有连接到Heroku的数据库都必须使用TLS/SSL。 - idej1234
URL已失效。对于Vapor 3,请访问以下链接:https://github.com/vapor/postgres-kit/blob/1/Sources/PostgreSQL/Connection/PostgreSQLConnection%2BTransportConfig.swift - Justin Oroz

1

Vapor 4 + 构建堆栈 heroku-20 + Heroku Postgres 的标准计划

Rijel David 的建议解决了我的问题

但是 unverifiedTLS 语法略有变化

if let databaseURL = Environment.get("DATABASE_URL"), var postgresConfig = PostgresConfiguration(url: databaseURL) {
    postgresConfig.tlsConfiguration = .forClient(certificateVerification: .none)
    app.databases.use(.postgres(
        configuration: postgresConfig
    ), as: .psql)
} else {
    // ...
}

请查看Vapor文档 - https://docs.vapor.codes/4.0/deploy/heroku/


1

这是一个花费了我很多个人成本的过程,这个解决方案对我有效,你可以尝试一下。


在文件Config > secrets > postgresql.json中添加以下配置(用于本地或远程使用,如果该文件不存在,请创建此文件).
{
   "host": "127.0.0.1",
   "user": "your_user_pc", 
   "password": "",
   "database": "your_user_pc",
   "port": 5432
}

用户可以从终端获取它

$ cd ~

在您的文件Procfile上(位于您的项目中,通过Finder显示),编辑并添加以下代码。
web: App --env=production --workdir="./"
web: App --env=production --workdir=./ --config:servers.default.port=$PORT --config:postgresql.url=$DATABASE_URL

现在您可以重新启动应用程序到heroku,您应该考虑从Heroku界面使用所有凭据和Postgresql的附加组件正确配置服务器。
注意:不要忘记每次进行更改后运行“vapor build”或“vapor build --clean”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接