Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

494
Visualizações
How can I connect to a Cloud PostgreSQL database from dart code?

I have a PostgreSQL database deployed in Google Cloud that I am trying to connect to from a Cloud Run instance. I have tried the following two packages, both of them eventually leading to the same exception:

  • https://pub.dev/packages/postgres
  • https://pub.dev/packages/database_adapter_postgre

The exception I am getting is: SocketException: Failed host lookup: '/cloudsql/{INSTANCE_CONNECTION_NAME}' (OS Error: Name or service not known, errno = -2)

I get here both times when trying to establish the connection, so in the case of the first package:

connection = new PostgreSQLConnection(
        '/cloudsql/{INSTANCE_CONNECTION_NAME}',
        5432,
        'postgres',
        username: 'username',
        password: 'password');
await connection.open(); // <-- exception thrown here

I have tried changing the host string to /cloudsql/INSTANCE_CONNECTION_NAME}/.s.PGSQL.5432, but that did not work. My first thought were permissions, the service account the Cloud Run instance is using (xxx-compute@developer.gserviceaccount.com) has the Cloud SQL Editor role (tried Client and Admin too).

Running the same database code locally from a dart console app, I can connect to my database via its public IP address as the host with both packages, so the database itself is up and running.

Can someone point me in the right direction with this exception/have an example code for any of the packages above to show how to connect it to a Cloud SQL instance from a Cloud Run?

Edit: I tried setting up a proxy locally to test out if the connection is wrong like so:

.\cloud_sql_proxy.exe -instances={INSTANCE_CONNECTION_NAME}=tcp:5433 psql

Then changing the connection host value in the code to localhost, and the port to 5433. To my surprise it works - so from locally I am seemingly able to connect to the DB using that connection string. It still doesn't work when I use it from a Cloud Run instance though. Any help is appreciated!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It seems dart doesn't support connection through unix socket, you need to configure a IP (public or private, as you need).

Alternatively you can use pg which support unix socket connection

Hope this helps.

over 4 years ago · Santiago Trujillo Relatório

0

Just for those who come across this question in the future: as it stands right now, I had to resort to the suggestion posted by Daniele Ricci and use the public IP for the database. The one thing to point out here was that since Cloud Runs don't have a static IPv4 address to run from, the DB had to be set to allow connections from anywhere (had to add an authorized connection from 0.0.0.0/0), which is unsafe. Until the kind development team of dart figures out how to use UNIX sockets, this seems to be the only way of getting it to work.

over 4 years ago · Santiago Trujillo Relatório

0

Not having actually tested this myself, according to the source code of the postgres package, you have to specify that you want a Unix socket:

connection = PostgreSQLConnection(
  ...
  isUnixSocket: true, //              <-- here
);

The default is false.

The host you pass is must also be valid. The docs say:

[host] must be a hostname, e.g. "foobar.com" or IP address. Do not include scheme or port.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda