NextAuth.js comes with multiple ways of connecting to a database:
- TypeORM (default)
The TypeORM adapter supports MySQL, Postgres, MsSql, SQLite and MongoDB databases.
The Prisma 2 adapter supports MySQL, Postgres and SQLite databases.
- Custom Adapter
A custom Adapter can be used to connect to any database.
This document covers the default adapter (TypeORM).
See the documentation for adapters to learn more about using Prisma adapter or using a custom adapter.
To learn more about databases in NextAuth.js and how they are used, check out databases in the FAQ.
How to use a database
You can specify database credentials as as a connection string or a TypeORM configuration object.
The following approaches are exactly equivalent:
You can pass in any valid TypeORM configuration option.
e.g. To set a prefix for all table names you can use the entityPrefix option as connection string parameter:
…or as a database configuration object:
Setting up a database
Using SQL to create tables and columns is the recommended way to set up an SQL database for NextAuth.js.
Check out the links below for SQL you can run to set up a database for NextAuth.js.
If you are running SQLite, MongoDB or a Document database you can skip this step.
Alternatively, you can also have your database configured automatically using the
synchronize: true option:
synchronize option should not be used against production databases.
It is useful to create the tables you need when setting up a database for the first time, but it should not be enabled against production databases as it may result in data loss if there is a difference between the schema that found in the database and the schema that the version of NextAuth.js being used is expecting.
The default database adapter is TypeORM, but only some databases supported by TypeORM are supported by NextAuth.js as custom logic needs to be handled by NextAuth.js.
Databases compatible with MySQL, Postgres and MongoDB should work out of the box with NextAuth.js. When used with any other database, NextAuth.js will assume an ANSI SQL compatible database.
When configuring your database you also need to install an appropriate node module for your database.
npm i mysql
npm i mariadb
npm i pg
Microsoft SQL Server
npm i mssql
npm i mongodb
SQLite is intended only for development / testing and not for production use.
npm i sqlite3