使用 SQLAlchemy 通过 unix 套接字连接到数据库 [英] Connect to a database over a unix socket using SQLAlchemy

查看:30
本文介绍了使用 SQLAlchemy 通过 unix 套接字连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的云函数中的 SQLAlchemy 连接到我的 Cloud SQL DB,但我似乎无法计算出正确的连接字符串.

I'm trying to connect to my Cloud SQL DB using SQLAlchemy from my cloud function but I can't seem to work out the correct connection string.

DATABASE_URL=postgres://$DB_USER:$_DB_PWD@/$DB_NAME?unix_socket=/cloudsql/$DB_INSTANCE

这给了我错误:

pyscopg2.ProgrammingError: invalid dns: invalid connection option "unix_socket"

使用 pyscopg2 通过 unix 套接字连接到 Postgresql 9.6 DB 的正确方法是什么?

What is the correct way to connect to a Postgresql 9.6 DB over a unix socket using pyscopg2?

推荐答案

这里需要的特殊关键字是host:

The special keyword needed here is host:

DATABASE_URL=postgres://user:password@/dbname?host=/path/to/db

请注意,host 中的路径应该是路径,而不是套接字文件本身(psycopg2 假定套接字具有标准命名约定 .s.PGSQL.5432)

Note that the path in host should be a path, not the socket file itself (psycopg2 assumes the socket has the standard naming convention .s.PGSQL.5432)

https://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#unix-domain-connections

这篇关于使用 SQLAlchemy 通过 unix 套接字连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆