0

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.

wes kay
  • 97
  • 1
  • 8

2 Answers2

1

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]")
maxm
  • 3,412
  • 1
  • 19
  • 27
  • Removing the semicolons gives me another error: `Unable to connect to database: failed to connect to host=[my ip] user=postgres database=[my db]: dial error (dial tcp [my ip]: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)` – wes kay Mar 25 '21 at 23:09
1

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]")

wes kay
  • 97
  • 1
  • 8