使用golang连接到谷歌云虚拟机实例上的postgres



我正在使用Golang(github.com/jackc/pgx/v4(尝试连接到我的计算引擎VM实例,但在GCP上进行postgres安装后,"Unable to connect to database: failed to connect to host=[IP] user=postgres database=postgres: dial error (dial tcp 127.0.0.1:5433: connectex: No connection could be made because the target machine actively refused it.)"没有任何运气:https://cloud.google.com/community/tutorials/setting-up-postgres

在戈兰,我使用pgx.Connect(),这是我传递的DSN:dsn = "pgsql:host=[My external VM IP on GCP];port=5432;dbname=[DB name];user=postgres;password=[my pass]"

我有一种有趣的感觉,我需要连接到一个实例ID,但没有关于我在GCP上访问VM时传递的正确主机或正确DSN格式的文档。

错误消息看起来像是试图连接到127.0.0.1:5433

可以尝试从DSN中删除分号吗?

pgx.Connect(context.Background(), "host=[My external VM IP on GCP] port=5432 dbname=[DB name] user=postgres password=[my pass]")

似乎网络标签(在防火墙下的VM实例中找到的标签需要相同(正确的格式就像maxm建议的:pgx.Connect(context.Background(), "host=[My external VM IP on GCP] port=5432 dbname=[DB name] user=postgres password=[my pass]")

最新更新