<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i" -->

---
title: Cloudflare Hyperdrive tutorial: use PostgreSQL with Workers
description: Cloudflare Hyperdrive connects Workers to an existing PostgreSQL or MySQL database by pooling and optionally caching connections near the origin database. The...
canonical: https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Cloudflare Hyperdrive tutorial: use PostgreSQL with Workers | daily.dev
og:description: Cloudflare Hyperdrive connects Workers to an existing PostgreSQL or MySQL database by pooling and optionally caching connections near the origin database. The...
og:url: https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i
og:image: https://api.daily.dev/og/posts/7iZBDkP5i.png
og:image:alt: Cloudflare Hyperdrive tutorial: use PostgreSQL with Workers
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare Hyperdrive tutorial: use PostgreSQL with Workers

**[Flavio Copes](https://daily.dev/sources/flaviocopes)** · 24 min read · 32 upvotes · 6 comments

## Summary

Cloudflare Hyperdrive connects Workers to an existing PostgreSQL or MySQL database by pooling and optionally caching connections near the origin database. The tutorial builds a read-only notes API with the pg driver, configures a Hyperdrive binding, and tests it locally and after deployment. It covers enabling query caching, whose documented defaults are 60 seconds of cache lifetime plus 15 seconds of stale-while-revalidate, noting that writes don't invalidate cached results. It also explains transaction pooling, Drizzle and MySQL support, private VPC connectivity, and Workers Free plan limits of 100,000 queries per day versus unlimited on Paid.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://flaviocopes.com/cloudflare-hyperdrive>

## Questions this post answers

### What is Cloudflare Hyperdrive used for with PostgreSQL and Workers?

Cloudflare Hyperdrive connects Cloudflare Workers to an existing PostgreSQL or MySQL database by keeping long-lived origin connections ready to reuse and optionally caching read queries. The database stays where it is, such as a VPS, Supabase, Neon, or RDS, while Workers run the application. It does not replicate the database globally or speed up slow queries lacking proper indexes.

_Developers wiring Workers to an existing database can track Hyperdrive setup details on daily.dev._

### Does Hyperdrive's query cache invalidate automatically when the underlying data changes?

No, writes do not invalidate cached query results in Hyperdrive. The documented defaults are 60 seconds of cache lifetime plus 15 seconds of stale-while-revalidate, so a cached query can still return a row after it changes, such as a note that was unpublished. Use a cache-disabled Hyperdrive configuration for reads that must be fresh, like permission checks or account balances.

_Anyone debugging stale reads after a database update can compare caching trade-offs on daily.dev._

### How many database queries does the Cloudflare Workers Free plan allow through Hyperdrive per day?

The Workers Free plan includes 100,000 Hyperdrive database queries per day, while the Paid plan lists unlimited Hyperdrive queries, as checked on October 24, 2026. The query count covers all database statements, including writes and schema changes, and Workers execution plus the database provider still have their own separate charges and limits.

_Teams budgeting Workers and database usage can check current limits like this on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@swarnasharmin** · 10 upvotes

> Since writes don't automatically invalidate Hyperdrive's cached results, how do you handle stale-read race conditions in high-concurrency transactional workflows?

**@mehakulfat** · 7 upvotes

> Overall, Cloudflare Hyperdrive makes it easier to integrate PostgreSQL and MySQL databases with Cloudflare Workers. Its connection pooling, optional caching, and support for popular database tools make it a promising option for serverless applications. It is particularly valuable when developers want to improve database connectivity while keeping their existing database infrastructure.

**@devdavidt** · 1 upvotes

> your tutorials are really helpful

**@diyweb** · 0 upvotes

> The stale-read question is the right one. A practical rule is to cache only reads where being 60 seconds behind is acceptable, such as lists and public content, and send anything that follows a write by the same user straight to the origin.

**@rlaborantin** · 0 upvotes

> I'm going to follow the tutorial because I'm currently working on a project with the Workers + Psql + Neon

## Similar posts on daily.dev

- [Cloudflare Workers and Hyperdrive with TanStack Start – Master.dev Blog](https://daily.dev/posts/cloudflare-workers-and-hyperdrive-with-tanstack-start-master-dev-blog-nbss2iywk) · Frontend Masters · 0 upvotes · 0 comments
- [Deploy Postgres and MySQL databases with PlanetScale \+ Workers](https://daily.dev/posts/deploy-postgres-and-mysql-databases-with-planetscale-workers-115dairat) · Cloudflare · 1 upvotes · 0 comments
- [Cloudflare D1: a SQL database for your Workers](https://daily.dev/posts/cloudflare-d1-a-sql-database-for-your-workers-kkoyuhilj) · Flavio Copes · 1 upvotes · 0 comments
- [Cloudflare Workers and Hyperdrive with SvelteKit – Master.dev Blog](https://daily.dev/posts/cloudflare-workers-and-hyperdrive-with-sveltekit-master-dev-blog-pwsee8bhi) · Frontend Masters · 0 upvotes · 0 comments
- [MotherDuck on Cloudflare Workers: real-time apps at the edge](https://daily.dev/posts/motherduck-on-cloudflare-workers-real-time-apps-at-the-edge-caqfsv4vi) · MotherDuck · 0 upvotes · 0 comments

---

Tags: [#database](https://daily.dev/tags/database), [#postgresql](https://daily.dev/tags/postgresql), [#cloudflare](https://daily.dev/tags/cloudflare), [#drizzle](https://daily.dev/tags/drizzle)

[View this post on daily.dev](https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Cloudflare Hyperdrive tutorial: use PostgreSQL with Workers","url":"https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i"},"datePublished":"2026-10-25T11:21:44.062Z","dateModified":"2026-10-25T11:22:19.039Z","description":"Cloudflare Hyperdrive connects Workers to an existing PostgreSQL or MySQL database by pooling and optionally caching connections near the origin database. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/4167973f753aaeaee6fe8980abcec3bb?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/4167973f753aaeaee6fe8980abcec3bb?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Flavio Copes","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Flavio Copes","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/1f2dbd275797411bbd37bfdaaf0951a9","url":"https://daily.dev/sources/flaviocopes"},"commentCount":6,"discussionUrl":"https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":32},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":6}],"keywords":"database,postgresql,cloudflare,drizzle","timeRequired":"PT24M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Flavio Copes","item":"https://daily.dev/sources/flaviocopes"},{"@type":"ListItem","position":3,"name":"Cloudflare Hyperdrive tutorial: use PostgreSQL with Workers"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i","comment":[{"@type":"Comment","text":"Since writes don’t automatically invalidate Hyperdrive’s cached results, how do you handle stale-read race conditions in high-concurrency transactional workflows?","datePublished":"2026-10-26T04:33:19.369Z","url":"https://daily.dev/posts/7iZBDkP5i#c-bPc6VWYD2","author":{"@type":"Person","name":"Swarna Sharmin","url":"https://daily.dev/swarnasharmin","image":"https://media.daily.dev/image/upload/s--joHEq_jD--/f_auto/v1791022797/avatars/avatar_ZL6bebyCVkszL2cr0g8ab?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":10}},{"@type":"Comment","text":"Overall, Cloudflare Hyperdrive makes it easier to integrate PostgreSQL and MySQL databases with Cloudflare Workers. Its connection pooling, optional caching, and support for popular database tools make it a promising option for serverless applications. It is particularly valuable when developers want to improve database connectivity while keeping their existing database infrastructure.","datePublished":"2026-10-26T04:46:21.187Z","url":"https://daily.dev/posts/7iZBDkP5i#c-XDdI78qYX","author":{"@type":"Person","name":"Mehak Ulfat","url":"https://daily.dev/mehakulfat","image":"https://lh3.googleusercontent.com/a/ACg8ocL_fsXZhdOFAKx7AdI7pUJBUg-eKU06JcQaFuvSpwKLS-jbT_I=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":7}},{"@type":"Comment","text":"your tutorials are really helpful","datePublished":"2026-10-27T10:49:22.078Z","url":"https://daily.dev/posts/7iZBDkP5i#c-0QJmbwWXZ","author":{"@type":"Person","name":"DavidDev","url":"https://daily.dev/devdavidt","image":"https://media.daily.dev/image/upload/s--jxmKsIov--/f_auto/v1788950401/avatars/avatar_HK0Qdu9ttIpcoDHx7zrHk?_a=BAMAMicg0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"The stale-read question is the right one. A practical rule is to cache only reads where being 60 seconds behind is acceptable, such as lists and public content, and send anything that follows a write by the same user straight to the origin.","datePublished":"2026-10-27T13:42:34.830Z","url":"https://daily.dev/posts/7iZBDkP5i#c-MbrWYb4S7","author":{"@type":"Person","name":"DiyWeb","url":"https://daily.dev/diyweb","image":"https://media.daily.dev/image/upload/s--dgS6jDeN--/f_auto/v1790879420/avatars/avatar_774zDwMaRTNS1fv5OKStX?_a=BAMAMicg0"}},{"@type":"Comment","text":"I’m going to follow the tutorial because I’m currently working on a project with the Workers + Psql + Neon","datePublished":"2026-10-28T10:13:34.125Z","url":"https://daily.dev/posts/7iZBDkP5i#c-G8nccTGSS","author":{"@type":"Person","name":"Rachid Le Laborantin","url":"https://daily.dev/rlaborantin","image":"https://avatars.githubusercontent.com/u/81116574?v=4"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/cloudflare-hyperdrive-tutorial-use-postgresql-with-workers-7izbdkp5i#faq","mainEntity":[{"@type":"Question","name":"What is Cloudflare Hyperdrive used for with PostgreSQL and Workers?","acceptedAnswer":{"@type":"Answer","text":"Cloudflare Hyperdrive connects Cloudflare Workers to an existing PostgreSQL or MySQL database by keeping long-lived origin connections ready to reuse and optionally caching read queries. The database stays where it is, such as a VPS, Supabase, Neon, or RDS, while Workers run the application. It does not replicate the database globally or speed up slow queries lacking proper indexes. Developers wiring Workers to an existing database can track Hyperdrive setup details on daily.dev."}},{"@type":"Question","name":"Does Hyperdrive's query cache invalidate automatically when the underlying data changes?","acceptedAnswer":{"@type":"Answer","text":"No, writes do not invalidate cached query results in Hyperdrive. The documented defaults are 60 seconds of cache lifetime plus 15 seconds of stale-while-revalidate, so a cached query can still return a row after it changes, such as a note that was unpublished. Use a cache-disabled Hyperdrive configuration for reads that must be fresh, like permission checks or account balances. Anyone debugging stale reads after a database update can compare caching trade-offs on daily.dev."}},{"@type":"Question","name":"How many database queries does the Cloudflare Workers Free plan allow through Hyperdrive per day?","acceptedAnswer":{"@type":"Answer","text":"The Workers Free plan includes 100,000 Hyperdrive database queries per day, while the Paid plan lists unlimited Hyperdrive queries, as checked on October 24, 2026. The query count covers all database statements, including writes and schema changes, and Workers execution plus the database provider still have their own separate charges and limits. Teams budgeting Workers and database usage can check current limits like this on daily.dev."}}]}
```

