Connection, pool and logging
One Sequelize instance owns one connection pool (or one pool per replica, see read replicas). Create it once, share it across the application and call sequelize.close() on shutdown. Every option of the constructor is listed with its type and default in the connection option reference; this page shows how they fit together.
Connection URI
A URI carries host, port, database and credentials. Options given next to it override the values in the URI:
const fromUrl = new Sequelize(url, { logging: false, dialectOptions });
await fromUrl.authenticate(); // throws a ConnectionError subclass when the server is unreachable
await fromUrl.close();authenticate() runs SELECT 1 and rejects with a ConnectionError subclass when the server cannot be reached, so it is a good startup check.
Options object
The same settings can be given as separate options, together with the pool, session and model defaults:
const fromOptions = new Sequelize({
host,
port,
database,
username,
password,
dialectOptions,
logging: false,
timezone: '+00:00', // session time zone, the default
define: { underscored: true, freezeTableName: true }, // defaults for every model
pool: { max: 10, idle: 10_000, acquire: 30_000 },
});| Option | What it does |
|---|---|
dialect | Optional; the only accepted value is 'postgres'. |
ssl | TLS settings passed to pg (true, or { ca, rejectUnauthorized, ... }). dialectOptions takes any other pg pool setting. |
pool | max (default 5), idle (10 000 ms), acquire (60 000 ms, then ConnectionAcquireTimeoutError), maxUses. min and evict are accepted and ignored. |
timezone | Session time zone; default '+00:00'. |
define | Defaults for every model: timestamps, underscored, freezeTableName, paranoid, ... |
minifyAliases | Shorten generated aliases of deep includes to stay inside PostgreSQL's 63-byte identifier limit. |
hooks | Sequelize-level hooks for every model. |
retry | Retry failed connection acquisition and transaction starts; see retrying. |
replication | Read replicas and a writer; see read replicas. |
statementCache postgrelize extension | Opt-in named prepared statements, bounded per connection; see performance. |
inAsAnyThreshold postgrelize extension | Length from which IN (...) lists compile to = ANY($1), so one statement text serves every list length. |
Logging
logging receives the SQL text of every executed query with the values inlined as literals, like Sequelize v6, so a logged statement can be run as it is. The query itself is still sent to the server with bound parameters; the second argument is { bind, parameterizedSql } (the bound values and the statement as sent, $1, $2, …; parameterizedSql is a postgrelize extension). Use false to silence it and benchmark: true to receive the duration in milliseconds as the second argument. A per-query logging option overrides the instance value.
const statements: string[] = [];
const logged = new Sequelize({
host,
port,
database,
username,
password,
dialectOptions,
logging: (sql) => statements.push(sql), // called with the SQL text of every query
benchmark: true, // also pass the duration in ms as the second argument
});
await logged.query('SELECT 1');
await logged.close();Wrapping sequelize.query
Like in Sequelize v6, the queries of models, instances and the QueryInterface run through the instance method sequelize.query, so a wrapper or an override of it sees the SQL of every statement (to add a route comment, to measure, to trace). The wrapper receives the SQL as a string and the bound values in options.bind; it must hand options on to the original method unchanged (the options carry what tells query that the statement comes from a model). A wrapper written for v6 that also handles a { query, bind } object as the first argument works with both libraries.
const original = sequelize.query.bind(sequelize);
sequelize.query = (sql, options) =>
original(typeof sql === 'string' ? `/* route:web */ ${sql}` : sql, options);Prepared statements and long IN lists postgrelize extension
Two constructor options exist only in postgrelize. statementCache turns on named prepared statements, inAsAnyThreshold makes long IN lists use one statement shape:
const cached = new Sequelize({
host,
port,
database,
username,
password,
dialectOptions,
logging: false,
statementCache: { max: 50 }, // postgrelize extension: named prepared statements, 50 per connection
inAsAnyThreshold: 50, // postgrelize extension: IN lists of 50+ values compile to = ANY($1)
});Pool counters and shutdown
sequelize.connectionManager.pool reports live size, available, using, waiting and maxSize; useful for health checks. The routing and raw-connection details are in Sessions and scaling.
const Note = fromOptions.define('Note', { body: DataTypes.TEXT });
await Note.sync({ force: true }); // freezeTableName: the table is "Note", columns are snake_case
const { size, using } = fromOptions.connectionManager.pool; // live pool counters
await fromOptions.close(); // call it on shutdown; pending connections are closed