在Google Cloud SQL上发布REST:unix套接字URI格式



你们有PostgREST和Cloud SQL的经验吗?

我已经准备好了我的SQL实例的开放访问(0.0.0.0/0(,我可以使用云代理应用程序通过本地PostGREST访问它。

现在我想从同一个项目的实例运行Postgres,但是我找不到支持云SQL格式的Postgres的URI格式,因为Google SQL Cloud只使用像/cloudsql/INSTANCE_CONNECTION_NAME 这样的unix套接字

配置1

db-uri = "postgres://postgres:password@/unix(/cloudsql/INSTANCE_CONNECTION_NAME)/mydatabase"
db-schema = "api"
jwt-secret = "OOcJ7VoSY1mXqod4MKtb9WCCwt9erJkRQ2tzYmLb4Xe="
db-anon-role = "web_anon"
server-port=3000

返回{"details":"could not translate host name "unix(" to address: Unknown hostn","code":"","message":"Database connection error"}

配置2

db-uri = "postgres://postgres:password@/mydatabase?unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME"
db-schema = "api"
jwt-secret = "OOcJ7VoSY1mXqod4MKtb9WCCwt9erJkRQ2tzYmLb4Xe="
db-anon-role = "web_anon"
server-port=3000

解析程序拒绝问号{"details":"invalid URI query parameter: "unix_socket"n","code":"","message":"Database connection error"}

配置3

db-uri = "postgres://postgres:password@/mydatabase"
db-schema = "api"
jwt-secret = "OOcJ7VoSY1mXqod4MKtb9WCCwt9erJkRQ2tzYmLb4Xe="
db-anon-role = "web_anon"
server-port=3000
server-unix-socket= "/cloudsql/INSTANCE_CONNECTION_NAME"

server-unix-socket似乎只采用套接字锁定文件路径。/cloudsql/INSTANCE_CONNECTION_NAME尝试删除文件,如`postgrest.exe:/cloudsql/INSTANCE_CONNECTION_NAME:DeleteFile"/cloudsql/INSTANCE_COCONNECTION_NAME":无效参数t(文件名、目录名或卷标语法不正确(

文件

云SQL文档

  • https://cloud.google.com/sql/docs/mysql/connect-run

PostgREST

  • http://postgrest.org/en/v6.0/configuration.html
  • https://github.com/PostgREST/postgrest/issues/1186
  • https://github.com/PostgREST/postgrest/issues/169

环境

  • PostgreSQL版本:11
  • PostgREST版本:6.0.2
  • 操作系统:Win10和Alpine

首先必须将Cloud SQL连接添加到Cloud Run实例:https://cloud.google.com/sql/docs/postgres/connect-run#configuring

之后,DB连接将在路径/cloudsql/<cloud_sql_instance_connection_name>的Unix域套接字上的服务中可用,您可以设置PGRST_DB_URI环境变量来反映这一点。

以下是正确的格式:postgres://<pg_user>:<pg_pass>@/<db_name>?host=/cloudsql/<cloud_sql_instance_connection_name>

例如。postgres://postgres:postgres@/postgres?host=/cloudsql/project-id:zone-id-1:sql-instance

根据与CloudSQL的连接,示例为:

# postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_sock=/cloudsql//.s.PGSQL.5432

然后你可以试试(正如@marian.valdoi提到的(:

db-uri = "postgres://postgres:password@/mydatabase?unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME/.s.PGSQL.5432"

请记住,连接名称应包括:

ProjectID:Region:DatabaseName

例如:myproject:myregion:myinstance

无论如何,你可以在这里找到更多从外部应用程序和谷歌云连接的选项。

我尝试了许多变体,但无法开箱即用,但我将发布此解决方法。

FWIW我可以在本地使用postgrest的备用套接字位置,但当尝试使用cloudsql位置时,它似乎没有正确解释它——也许套接字路径中的冒号会把它扔掉?

在任何情况下,正如@Steven_Chávez所提到的,这种方法确实适用于db-uri = postgres:///user:password@/dbname,并默认为postgrest默认套接字位置(/run/postgresql/.s.PGSQL.5432(。因此,在docker入口点中,我们可以将此位置符号链接到Cloud Run注入的实际套接字。

首先,将以下内容添加到Dockerfile(USER 1000之上(:

RUN mkdir -p /run/postgresql/ && chown postgrest:postgrest /run/postgresql/

然后在/etc/entrypoint.bash添加一个包含以下内容的可执行文件:

set -eEux pipefail
CLOUDSQL_INSTANCE_NAME=${CLOUDSQL_INSTANCE_NAME:-PROJECT_REGION_INSTANCE_NAME}
POSTGRES_SOCKET_LOCATION=/run/postgresql
ln -s /cloudsql/${CLOUDSQL_INSTANCE_NAME}/.s.PGSQL.5432 ${POSTGRES_SOCKET_LOCATION}/.s.PGSQL.5432 
postgrest /etc/postgrest.conf

将Dockefile入口点更改为CMD /etc/entrypoint.sh。然后在cloudrun中添加CLOUDSQL_INSTANCE_NAME作为env-var。PGRST_DB_URI env var与postgres://authenticator:password@/postgres 类似

如果您不喜欢这样,另一种方法是通过无服务器vpc连接器进行连接。

我也很难做到这一点。

我最终为DB-URI env变量做了一行

host=/cloudsql/project-id:zone:instance-id user=user port=5432 dbname=dbname password=password

然而,我有在云上运行的postgrest,它允许您通过指定实例连接名称

INSTANCE_CONNECTION_NAME=/cloudsql/项目id:区域:实例id

也许你可以在那里托管它,但最终却没有服务器。我不确定你目前在哪里运行它。

https://cloud.google.com/sql/docs/mysql/connect-run

最新更新