gpt4 book ai didi

postgresql - 您如何指定 DB_URI postgres DB 连接字符串到在 Google Sql 云中运行的实例?

转载 作者:行者123 更新时间:2023-12-04 16:42:10 24 4
gpt4 key购买 nike

这是我的场景。我已经设置了一个在 Google SQL 云中运行的 Postgres DB 实例。它已启动并正在运行,如果我将本地 IP 列入白名单,我可以毫无问题地直接连接。

然后我部署了一个 docker 容器 (postGrest),它是一个连接到 postgres 数据库的 Web 服务器。
当我在 Google Cloud Run 上配置它时,它确实有一个下拉选项,我可以在其中指定数据库连接,它说,在幕后,它为此连接配置 Cloud SQL 代理。

容器允许传入环境变量来指定哪个服务器等。
一个必需的参数是 postgred 实例的 DB_URI。在本地运行时,它看起来像这样:

postgres://authenticator:mysecretpassword@localhost:5432/testdb

当我尝试在云版本上配置它时,我尝试使用 IP 127.0.0.1(谷歌云 SQL 代理文档说这是通过代理连接的方式)。这没有用。

然后我尝试使用分配给 postgres 数据库的公共(public) ip ......这也不起作用。

有谁知道如何使用这种 DB_URI 格式指定正确的连接字符串?

最佳答案

我只是将其添加为答案而不是评论,因为它更易于阅读并且可能会帮助其他用户。请不要鼓励更改已接受的答案。
通过关注 the documentation provided by the OP ,URI 的最终模式变为:

# Breaking lines for improved readability

POSTGRESS_URI=postgresql:///dbname
?host=/cloudsql/myprojectid:region:myinstanceid
&user=username
&password=password
&sslmode=disable

* 不要忘记在 unix 套接字路径前面加上 /cloudsql/任何参数都可以正常使用,如 sslmode的例子.
另外,请注意, Cloud SQL documentation 中提到了两件重要的事情。 :
  1. Note: The PostgreSQL standard requires a .s.PGSQL.5432 suffix in the socket path. Some libraries apply this suffix automatically, but others require you to specify the socket path as follows: /cloudsql/INSTANCE_CONNECTION_NAME/.s.PGSQL.5432.

就我而言,我使用的程序已经添加了后缀 .s.PGSQL.5432 ,所以我不需要将它添加到我的 URI 中。
  1. Warning: Linux based operating systems have a maximum socket path length of 107 characters. If the total length of the path exceeds this length, you will not be able to connect with a socket from Cloud Run (fully managed).

关于postgresql - 您如何指定 DB_URI postgres DB 连接字符串到在 Google Sql 云中运行的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58331055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com