如何为pgx.pool编写一个连接字符串以连接到远程RDS?

huangapple go评论88阅读模式
英文:

How to write a go connection string for pgx.pool to a remote RDS?

问题

我没有问题将go pgxpool连接到一个在docker容器中的postgresql数据库,但是无法弄清楚如何为linode postgresql RDS编写连接URL字符串。具体来说,URL的第一部分是什么意思"postgres://"?我找不到除了本地数据库之外的连接URL的示例,也找不到DSN连接的代码示例。

有人可以帮我提供连接URL或者DSN的详细信息吗?

这是我当前的连接字符串,返回"host is invalid"。ssl_mode也是无效的。

config, err := pgxpool.ParseConfig("user=linpostgres, password=secret, host=lin-9930-2356-pgsql-primary.servers.linodedb.net, port=5432 dbname=mydb, pool_max_conns=10")

这个psq连接字符串超时:"psql --username=linpostgres --host=lin-9930-2356-pgsql-primary.servers.linodedb.net port=5432 --password"

英文:

I have no trouble connecting go pgxpool to a postgresql database in a docker container but can't figure out how to write a connection URL string for a linode postgresql RDS. Specifically, what is the first part of the URL "postgres://"? I can't find any example for a connection URL other than a local db and no code examples for a DSN connection.

Can somebody please help me out with either a connection URL or DSN for these details?

Here is my current connection string which returns "host is invalid". ssl_mode is also invalid.

config, err := pgxpool.ParseConfig("user=linpostgres, password=secret, host=lin-9930-2356-pgsql-primary.servers.linodedb.net, port=5432 dbname=mydb, pool_max_conns=10")

This psq connect string times out: psql --username=linpostgres --host=lin-9930-2356-pgsql-primary.servers.linodedb.net port=5432 --password

答案1

得分: 2

你可以在这里查看官方GitHub存储库的代码:链接

// 请参考Config中这些参数的定义。
//
//	# 示例 DSN
//	user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10
//
//	# 示例 URL
//	postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10

所以你的连接字符串应该是

postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10

希望对你有所帮助。

英文:

You can check the code official GitHub repository code here: link

// See Config for definitions of these arguments.
//
//	# Example DSN 
//	user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10
//
//	# Example URL
//	postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10

so your connection string should be

postgres://jack:secret@pg.example.com:5432/mydb?sslmode=verify-ca&pool_max_conns=10

hope this helps

huangapple
  • 本文由 发表于 2022年10月8日 00:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/73989811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定