Trying to connect to GCP CloudSQL Postgres databsae externally outside of GCP and am trying to use the Cloud SQL Node.js Connector package.
I am using Sequelize as my ORM to connect to my postgres database, and when I use the examaples to add in the connection options, my database throws a connection error.
However when i try and connect directly to the databse using the pg package and use add in the connection options from the Cloud SQL Connector package, I am able to connect to the database and run queries.
The issue is that i want to use sequelize and sequelize typescript for the models, in the docs it says - Sequelize examples require Sequelize >=v7.0.0-alpha.27 (npm install @sequelize/core). Using this package instead of the default sequelize package allows me to connect, but i can't use the sequelize-typescript package with this version.
Has anyone had the same issue with connecting to a GCP Cloud SQL database?
Here is some code examples to show what
const connector = new Connector()
const clientOpts = await connector.getOptions({
instanceConnectionName: "instanceConnectionName",
ipType: IpAddressTypes.PUBLIC,
authType: AuthTypes.PASSWORD
})
// Connect through sequelize
// Using "@sequelize/core" this works but using "sequelize" and "sequelize-typescript" this fails
const sequelize = new Sequelize({
dialect: "postgres",
dialectModule: pg,
username: "postgres",
password: "password",
database: "database",
dialectOptions: {
...clientOpts
}
})
await sequelize.authenticate()
// Connect direct via "pg" - this works
const { Pool } = pg
const pool = new Pool({
...clientOpts,
username: "postgres",
password: "password",
database: "database",
max: 5
})
const { rows } = await pool.query("SELECT * FROM public.users")
console.table(rows)
Currently using:
- "sequelize": "6.33.0"
- "sequelize-typescript": "2.1.5"
- "pg": "8.11.3"
- "pg-hstore": "2.3.4"
- "@google-cloud/cloud-sql-connector": "1.2.1"
As you stated in your question,
sequelize@6is NOT supported by the Cloud SQL Node.js Connector and will not work.You should remove the
sequelizeandsequelize-typescriptand add@sequelize/coreinstead:From a quick search, it looks like when using Sequelize 7+ (
@sequelize/core) thesequelize-typescriptpackage is no longer needed since the TypeScript decorators should already be included in that package.