Connect to databases
Cloudflare Workers can connect to and query your data in both SQL and NoSQL databases, including:
- Cloudflare's own D1, a serverless SQL-based database.
- Traditional hosted relational databases, including Postgres and MySQL, using Hyperdrive (recommended) to significantly speed up access.
- Serverless databases, including Supabase, MongoDB Atlas, PlanetScale, FaunaDB, and Prisma.
D1 is Cloudflare's own SQL-based, serverless database. It is optimized for global access from Workers, and can scale out with multiple, smaller (10GB) databases, such as per-user, per-tenant or per-entity databases. Similar to some serverless databases, D1 pricing is based on query and storage costs.
Database | Library or Driver | Connection Method |
---|---|---|
D1 | Workers binding, integrates with Prisma ↗, Drizzle ↗, and other ORMs | Workers binding, REST API |
Traditional databases use SQL drivers that use TCP sockets to connect to the database. TCP is the de-facto standard protocol that many databases, such as PostgreSQL and MySQL, use for client connectivity. These drivers are also widely compatible with your preferred ORM libraries and query builders.
This also includes serverless databases that are PostgreSQL or MySQL-compatible like Supabase, Neon or PlanetScale, which can be connected to using both native TCP sockets and Hyperdrive or serverless HTTP-based drivers (detailed below).
Database | Integration | Library or Driver | Connection Method |
---|---|---|---|
Postgres | Direct connection | Postgres.js ↗,node-postgres ↗ | TCP Socket via database driver, using Hyperdrive for optimal performance (optional, recommended) |
MySQL | Direct connection | mysql2 ↗, mysql ↗ | TCP Socket via database driver, using Hyperdrive for optimal performance (optional, recommended) |
Serverless databases provide HTTP-based proxies and drivers, also known as serverless drivers. These address the lack of connection reuse between Worker invocation similarly to Hyperdrive for traditional SQL databases.
By providing a way to query your database with HTTP, these serverless databases and drivers eliminate several roundtrips needed to establish a secure connection.
Database | Integration | Library or Driver | Connection Method |
---|---|---|---|
Fauna ↗ | Yes | fauna ↗ | API through client library |
PlanetScale ↗ | Yes | @planetscale/database ↗ | API via client library |
Supabase ↗ | Yes | @supabase/supabase-js ↗ | API via client library |
Prisma ↗ | No | prisma ↗ | API via client library |
Neon ↗ | Yes | @neondatabase/serverless ↗ | API via client library |
Hasura ↗ | No | API | GraphQL API via fetch() |
Upstash Redis ↗ | Yes | @upstash/redis ↗ | API via client library |
TiDB Cloud ↗ | No | @tidbcloud/serverless ↗ | API via client library |
Once you have installed the necessary packages, use the APIs provided by these packages to connect to your database and perform operations on it. Refer to detailed links for service-specific instructions.
If your database requires authentication, use Wrangler secrets to securely store your credentials. To do this, create a secret in your Cloudflare Workers project using the following wrangler secret
command:
wrangler secret put <SECRET_NAME>
Then, retrieve the secret value in your code using the following code snippet:
const secretValue = env.<SECRET_NAME>;
Use the secret value to authenticate with the external service. For example, if the external service requires an API key or database username and password for authentication, include these in using the relevant service's library or API.
For services that require mTLS authentication, use mTLS certificates to present a client certificate.
- Learn how to connect to an existing PostgreSQL database with Hyperdrive.
- Discover other storage options available for use with Workers.
- Create your first database with Cloudflare D1.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark