Currently, our api (deployed on cloudRun) connects to our Postgres database by passing in a pgConfig with a db configuration and a db user and password.
For example:
const configObject = {
host: cloudRunHost,
user: dbUser,
password: dbPassword,
database: dbName
}
async function connect() {
if(!client) {
const pgPool = new pg.Pool(configObject);
await pgPool.connect()
.then((result) => {
logger.info('Connected to DB')
client = result;
}).catch((err) => {
logger.error(err);
});
}
}
We want the app itself to connect to the database by using Cloud SQL IAM database authentication
So far:
- The api cloudRun instance has a service account
- The database and CloudSQL has been configured for IAM-based access (we can access with our machine service accounts)
- The api service account has access to the DB via IAM, and permissions granted to said user on the Postgres db itself
- When the above code runs, it logs
error: empty password returned by client
We've tried removing the password line entirely from the configObject but it hasn't helped. Any thoughts on why my service account can access the db directly but the api's can't I suspect we need to indicate to pgPool that we're trying to connect via IAM instead of via user/password.
While @kurtisvg answer was correct at that point in time. I'd like to call attention to anyone ending up in this question that there is now a Cloud SQL Node.js Connector available that will enable IAM authentication while also removing the need for an extra proxy process.