Skip to main content
Version: v7 - alpha

Sequelize for PostgreSQL

Version Compatibility

See Releases to see which versions of PostgreSQL are supported.

To use Sequelize with PostgreSQL, you need to install the @sequelize/postgres dialect package:

npm i @sequelize/postgres

Then use the PostgresDialect class as the dialect option in the Sequelize constructor:

import { Sequelize } from '@sequelize/core';
import { PostgresDialect } from '@sequelize/postgres';

const sequelize = new Sequelize({
dialect: PostgresDialect,
database: 'mydb',
user: 'myuser',
password: 'mypass',
host: 'localhost',
port: 5432,
ssl: true,
clientMinMessages: 'notice',
});

Connection Options

Connection Options are used to configure a connection to the database.

The simplest way to use them is at the root of the configuration object. These options can also be used in the replication option to customize the connection for each replica, and can be modified by the beforeConnect hook on a connection-by-connection basis.

The following options are accepted by the PostgreSQL dialect:

OptionDescription
databaseThe name of the database to connect to.
userThe user to authenticate as.
passwordThe user's password.
hostThe host to connect to (either an IP, a domain name, or a path to a Unix socket).
portThe port to connect to. Default is 5432.
sslThe SSL configuration to use when connecting to the server. Passed directly to TLSSocket, supports all tls.connect options
query_timeoutThe number of milliseconds before a query call will timeout, default is no timeout.
connectionTimeoutMillisThe number of milliseconds to wait for a connection, default is no timeout.
application_nameConfigures the application_name PostgreSQL option for the connection.
statement_timeoutConfigures the statement_timeout PostgreSQL option for the connection.
idle_in_transaction_session_timeoutConfigures the idle_in_transaction_session_timeout PostgreSQL option for the connection.
client_encodingConfigures the client_encoding PostgreSQL option for the connection. Default is utf8.
lock_timeoutConfigures the lock_timeout PostgreSQL option for the connection.
optionsConfigures the options libpq option for the connection.
keepAliveConfigures the libpq keepalives option. Must be a boolean.
keepAliveInitialDelayMillisConfigures the libpq keepalives_idle option, but in milliseconds (will be rounded down to the nearest second).
info

Sequelize uses the pg package to connect to PostgreSQL. Most of the above options are provided as-is to the pg package, and you can find more information about them in the pg documentation.

Connection via Unix Socket

To connect to PostgreSQL using a Unix socket, you can use the host option with the absolute path to the socket file:

const sequelize = new Sequelize({
dialect: PostgresDialect,
host: '/var/run/postgresql',
});

Other PostgreSQL Options

The following options are also available for PostgreSQL:

OptionDescription
clientMinMessagesConfigures the client_min_messages PostgreSQL option for all connections. Defaults to "warning".
standardConformingStringsConfigures the standard_conforming_strings PostgreSQL option for all connections. If your PostgreSQL server is configured with standard_conforming_strings = off, it is extremely important to set this option to false to avoid SQL injection vulnerabilities.
nativeIf true, Sequelize will use the pg-native package instead of the pg package. pg-native must be installed separately.

Amazon Redshift

caution

While Redshift is based on PostgreSQL, it does not support the same set of features as PostgreSQL.

As we do not have access to a Redshift instance, we cannot guarantee that Sequelize will work correctly with Redshift, and we rely on the help of the community to keep this documentation up to date.

Redshift doesn't support client_min_messages, you must set it to 'ignore':

new Sequelize({
dialect: PostgresDialect,
// Your pg options here
clientMinMessages: 'ignore',
});