Connect to databases
Cloudflare Workers can connect to and query your data in both SQL and NoSQL databases, including:
- Traditional hosted relational databases, including Postgres and MySQL.
- Serverless databases: Supabase, MongoDB Atlas, PlanetScale, FaunaDB, and Prisma.
- Cloudflare’s own , a serverless SQL-based database.
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.
Connect to a database from a Worker
There are four ways to connect to a database from a Worker:
- With (recommended), which dramatically speeds up accessing traditional databases. Hyperdrive currently supports PostgreSQL and PostgreSQL-compatible database providers.
- : Simplifies authentication by managing credentials on your behalf and includes support for PlanetScale, Neon and Supabase.
- : A direct TCP connection to a database. TCP is the de-facto standard protocol that many databases, such as PostgreSQL and MySQL, use for client connectivity.
- HTTP- or WebSocket-based serverless drivers: Many hosted databases support a HTTP or WebSocket API to enable either clients to connect from environments that do not support TCP, or as their preferred connection protocol.
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.