1
votes

I'm trying to deploy postgresDemo function from node.js samples. But i'm not having success on get connection to my Cloud SQL instance.

I used the following command to publish the function: gcloud beta functions deploy postgresDemo --runtime nodejs8 --env-vars-file env.yaml --trigger-http

I added console log to see if all needed env vars are beeing filled correctly and it's fine. But when I try to create the pg.Pool, i have the following error:

Error: connect ECONNREFUSED 127.0.0.1:5432\n at Object._errnoException (util.js:1022:11)\n at _exceptionWithHostPort (util.js:1044:20)\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)\n code: 'ECONNREFUSED',\n errno: 'ECONNREFUSED',\n syscall: 'connect',\n address: '127.0.0.1',\n port: 5432

My App Engine Java application is working with this Cloud SQL instance. But in node cloud function i'm not havind success with the connection.

Here are my params only omitting user and password:

max: 1, user: 'username', password: 'password', database: 'appengine_helloworld_db', socketPath: '/cloudsql/hello-world-4736435:southamerica-east1:hello-world-postgis-sp'
2

2 Answers

1
votes

For anyone else who comes across this, I had to install and run the Cloud SQL Proxy: https://cloud.google.com/sql/docs/postgres/quickstart-proxy-test

Probably obvious, but if you jump right to the example you can miss it. For Cloud Functions, one potential source of errors is also what your active project is.

gcloud config list
0
votes

Postgres is also looking for the host parameter in your connection object if you're using knex. Adding that solved it for me:

const connectionName = 'your-project:region:instance-name';
const options = {
    client: 'pg',
    connection: {
        user: ....,
        password: ....,
        database: ....,
        host: `/cloudsql/${connectionName}`
    }
};
const knex = require('knex')(options);