Hi @ms4446 ,
Thanks for your response.
However, we are having the same issue. We now get this Sequelize error, which suggests the an authentication issue but can’t figure out why.
C:\Users\...\myApp\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:143
reject(new sequelizeErrors.ConnectionError(err));
^
ConnectionError [SequelizeConnectionError]: password authentication failed for user "grand-central-test@application-deployment-project.iam"
at Client._connectionCallback (C:\Users\...\myApp\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:143:24)
at Client._handleErrorWhileConnecting (C:\Users\...\myApp\node_modules\pg\lib\client.js:318:19)
at Client._handleErrorMessage (C:\Users\...\myApp\node_modules\pg\lib\client.js:338:19)
at Connection.emit (node:events:513:28)
at C:\Users\...\myApp\node_modules\pg\lib\connection.js:116:12
at Parser.parse (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:40:17)
at Socket.<anonymous> (C:\Users\...\myApp\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9) {
parent: error: password authentication failed for user "grand-central-test@application-deployment-project.iam"
at Parser.parseErrorMessage (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:39:38)
at Socket.<anonymous> (C:\Users\...\myApp\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 149,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '329',
routine: 'auth_failed'
},
original: error: password authentication failed for user "grand-central-test@application-deployment-project.iam"
at Parser.parseErrorMessage (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\...\myApp\node_modules\pg-protocol\dist\parser.js:39:38)
at Socket.<anonymous> (C:\Users\...\myApp\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 149,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '329',
routine: 'auth_failed'
}
}
GOOGLE_APPLICATION_CREDENTIALS has been added to our env varaibles and points to the service account key location. As suggested, we’ve removed the host from the sequelize config.
Here is the updated code:
// config.js
database: {
dialect: "postgres",
username: process.env.CLOUDSQL_DB_USERNAME,
password: process.env.CLOUDSQL_DB_PASSWORD,
database: process.env.CLOUDSQL_DB_DB,
migrationStorageTableName: "migrations",
logging: false, //console.log,
},
//database.js
const Sequelize = require("sequelize");
const config = require("config");
const { Connector } = require("@google-cloud/cloud-sql-connector");
const connector = new Connector();
const dbConfig = config.get("database");
const sequelize = new Sequelize(dbConfig);
sequelize.beforeConnect(async (config) => {
const clientOpts = await connector.getOptions({
instanceConnectionName: process.env.CLOUDSQL_INSTANCE_CONNECTION_NAME,
authType: "IAM",
});
if (process.env.NODE_ENV === "development") {
config = { ...config, ...clientOpts };
}
console.log(config);
});
// Test connection
try {
sequelize.authenticate();
console.log("Connection has been established successfully.");
} catch (err) {
console.error("Unable to connect to the database:", err);
}
module.exports = sequelize;
The console.log added in the beforeConnect hook returns the following:
{
database: 'database-name',
username: 'service-account-name@project-id.iam',
password: 'somePassword',
host: 'localhost',
port: 5432,
pool: {
max: 5,
min: 0,
idle: 10000,
acquire: 60000,
evict: 1000,
validate: [Function: bound _validate]
},
protocol: 'tcp',
native: false,
ssl: undefined,
replication: false,
dialectModule: null,
dialectModulePath: null,
keepDefaultTimezone: undefined,
dialectOptions: undefined,
stream: [Function: stream]
}
The service account was also added as a User to the cloudSQL instance.
In IAM, the service account has the following IAM roles :
Cloud SQL Client
Cloud SQL Instance User
Logs Writer.
Note: in the beforeConnect hook, Using
ipType: "PUBLIC"
instead of
authType: "IAM"
also returned the same error.
We tried removing the password from the config while following similar instructions in this codelabs but this returned the below error:
ConnectionError [SequelizeConnectionError]: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string