PostgreSQL
The PostgreSQL data source connector connects Prisma ORM to a PostgreSQL database server.
By default, the PostgreSQL connector contains a database driver responsible for connecting to your database. You can use a driver adapter (Preview) to connect to your database using a JavaScript database driver from Prisma Client.
Example
To connect to a PostgreSQL database server, you need to configure a datasource
block in your Prisma schema:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
The fields passed to the datasource
block are:
provider
: Specifies thepostgresql
data source connector.url
: Specifies the connection URL for the PostgreSQL database server. In this case, an environment variable is used to provide the connection URL.
Using the node-postgres
driver
As of v5.4.0
, you can use Prisma ORM with database drivers from the JavaScript ecosystem (instead of using Prisma ORM's built-in drivers). You can do this by using a driver adapter.
For PostgreSQL, node-postgres
(pg
) is one of the most popular drivers in the JavaScript ecosystem. It can be used with any PostgreSQL database that's accessed via TCP.
This section explains how you can use it with Prisma ORM and the @prisma/adapter-pg
driver adapter.
1. Enable the driverAdapters
Preview feature flag
Since driver adapters are currently in Preview, you need to enable its feature flag on the datasource
block in your Prisma schema:
// schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Once you have added the feature flag to your schema, re-generate Prisma Client:
npx prisma generate
2. Install the dependencies
Next, install the pg
package and Prisma ORM's driver adapter:
npm install pg
npm install @prisma/adapter-pg
3. Instantiate Prisma Client using the driver adapter
Finally, when you instantiate Prisma Client, you need to pass an instance of Prisma ORM's driver adapter to the PrismaClient
constructor:
import { Pool } from 'pg'
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '@prisma/client'
const connectionString = `${process.env.DATABASE_URL}`
const pool = new Pool({ connectionString })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })
Notice that this code requires the DATABASE_URL
environment variable to be set to your PostgreSQL connection string. You can learn more about the connection string below.
Notes
Specifying a PostgreSQL schema
You can specify a PostgreSQL schema by passing in the schema
option when instantiating PrismaPg
:
const adapter = new PrismaPg(pool, {
schema: 'myPostgresSchema'
})
Connection details
Connection URL
Prisma ORM follows the connection URL format specified by PostgreSQL's official guidelines, but does not support all arguments and includes additional arguments such as schema
. Here's an overview of the components needed for a PostgreSQL connection URL:
Base URL and path
Here is an example of the structure of the base URL and the path using placeholder values in uppercase letters:
postgresql://USER:PASSWORD@HOST:PORT/DATABASE
The following components make up the base URL of your database, they are always required:
Name | Placeholder | Description |
---|---|---|
Host | HOST | IP address/domain of your database server, e.g. localhost |
Port | PORT | Port on which your database server is running, e.g. 5432 |
User | USER | Name of your database user, e.g. janedoe |
Password | PASSWORD | Password for your database user |
Database | DATABASE | Name of the database you want to use, e.g. mydb |
You must percentage-encode special characters.
Arguments
A connection URL can also take arguments. Here is the same example from above with placeholder values in uppercase letters for three arguments:
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?KEY1=VALUE&KEY2=VALUE&KEY3=VALUE
The following arguments can be used:
Argument name | Required | Default | Description |
---|---|---|---|
schema | Yes | public | Name of the schema you want to use, e.g. myschema |
connection_limit | No | num_cpus * 2 + 1 | Maximum size of the connection pool |
connect_timeout | No | 5 | Maximum number of seconds to wait for a new connection to be opened, 0 means no timeout |
pool_timeout | No | 10 | Maximum number of seconds to wait for a new connection from the pool, 0 means no timeout |
sslmode | No | prefer | Configures whether to use TLS. Possible values: prefer , disable , require |
sslcert | No | Path of the server certificate. Certificate paths are resolved relative to the ./prisma folder | |
sslidentity | No | Path to the PKCS12 certificate | |
sslpassword | No | Password that was used to secure the PKCS12 file | |
sslaccept | No | accept_invalid_certs | Configures whether to check for missing values in the certificate. Possible values: accept_invalid_certs , strict |
host | No | Points to a directory that contains a socket to be used for the connection | |
socket_timeout | No | Maximum number of seconds to wait until a single query terminates | |
pgbouncer | No | false | Configure the Engine to enable PgBouncer compatibility mode |
statement_cache_size | No | 500 | Since 2.1.0: Specifies the number of prepared statements cached per connection |
application_name | No | Since 3.3.0: Specifies a value for the application_name configuration parameter | |
channel_binding | No | prefer | Since 4.8.0: Specifies a value for the channel_binding configuration parameter |
options | No | Since 3.8.0: Specifies command line options to send to the server at connection start |
As an example, if you want to connect to a schema called myschema
, set the connection pool size to 5
and configure a timeout for queries of 3
seconds. You can use the following arguments:
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=myschema&connection_limit=5&socket_timeout=3