The short of it is that I'm trying to write a migration service of sorts, triggered by Cloud Functions. I've followed the documentation here https://cloud.google.com/sql/docs/postgres/connect-functions, but Sequelize doesn't seem to be playing nicely with the environment variables that are to be set up. I do have my function deployed and the server set up and able to be connected to (it has a public IP).
I also tried out what this person discovered with their own config GCP Cloud Run Cloud - Cloud SQL instance "${process.env.INSTANCE_CONNECTION_NAME}" is not reachable, but still no luck. The most common error I'm recieving is:
ConnectionError [SequelizeConnectionError]: password authentication failed for user "postgres"
at Client._connectionCallback (/workspace/node_modules/sequelize/src/dialects/postgres/connection-manager.js:191:24)
at Client._handleErrorWhileConnecting (/workspace/node_modules/pg/lib/client.js:316:19)
at Client._handleErrorMessage (/workspace/node_modules/pg/lib/client.js:336:19)
at Connection.emit (node:events:513:28)
at Connection.emit (node:domain:552:15)
at /workspace/node_modules/pg/lib/connection.js:119:12
at Parser.parse (/workspace/node_modules/pg-protocol/src/parser.ts:104:9)
at Socket.<anonymous> (/workspace/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:513:28)
at Socket.emit (node:domain:552:15) {"
but this error most often pops up with an unconfigured postgres user, which I have verified is not the case by trying the credentials on the VM itself. I saw this error when I was working on the SQL Proxy, which I do have working locally. The Typescript code I'm trying to use to connect with from index.ts:
import 'ts-node/register';
import { Sequelize } from 'sequelize';
import { Umzug, SequelizeStorage } from 'umzug';
import * as dotenv from "dotenv";
dotenv.config();
const user = process.env.DB_USER!;
const host = process.env.DB_HOST!;
const database = process.env.DB_NAME!;
const password = process.env.DB_PASS!;
const port = process.env.DB_PORT ? process.env.DB_PORT as unknown as number: undefined;
const sequelize = new Sequelize(database, user, password, {
host,
port,
dialect: 'postgres',
logging: false,
dialectOptions: {
socketPath: process.env.INSTANCE_CONNECTION_NAME
}
});
(other unrelated logic)
export const migrate_core_db_up = async (req:any, res:any) => {
try {
await sequelize.authenticate();
res.send("Completed migration.");
} catch (e) {
console.error(e);
res.send("Failed to complete migration.");
}
}
My env vars are, from a yaml:
DB_NAME: db-name
DB_HOST: /cloudsql/project-id:zone:instance
INSTANCE_UNIX_SOCKET: /cloudsql/project-id:zone:instance
INSTANCE_CONNECTION_NAME: /cloudsql/project-id:zone:instance
DB_PASS: [password]
DB_USER: postgres
DB_PORT: 5432
And if it helps, the command that I'm using to deploy the function:
gcloud functions deploy migrate-core-db-up --gen2 --entry-point migrate_core_db_up --allow-unauthenticated --env-vars-file .env-functions.yaml \
--trigger-http --runtime nodejs18 --service-account=acc-name@proj-id.iam.gserviceaccount.com
I've toggled whether I use the port, removed the cloudsql prefix from connection name/host, and created and tried different users. I would really, really appreciate any pointers or suggestions! Is it even reasonable to try to latch onto the Cloud SQL Proxy within the deployed Function instance? The SSL certificate route seems equally perilous within Cloud SQL due to the lack of Node support based on the docs. Thanks in advance.