简体   繁体   中英

Connecting to postgres on google cloud VM instance with golang?

I'm using Golang (github.com/jackc/pgx/v4) to try and connect to my compute engine VM instance, but not having any luck, "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.)" after following the postgres install on GCP: https://cloud.google.com/community/tutorials/setting-up-postgres

In golang I'm using pgx.Connect() and this is the DSN I'm passing: dsn = "pgsql:host=[My external VM IP on GCP];port=5432;dbname=[DB name];user=postgres;password=[my pass]"

I have a funny feeling I need to connect to an instance ID, but there's no documentation on what's the correct host I pass or the right DSN format when trying to access a VM on GCP.

The error message looks like it's trying to connect to 127.0.0.1:5433 .

Can you try removing the semicolons from the DSN?

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

It seems the network tag, (that's found in the VM instance under the firewall needs to be the same) And the proper format is like maxm suggested: pgx.Connect(context.Background(), "host=[My external VM IP on GCP] port=5432 dbname=[DB name] user=postgres password=[my pass]")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM