You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
summary: Connect Hyperdrive to a PlanetScale PostgreSQL database.
4
+
pcx_content_type: example
5
+
title: PlanetScale
6
+
sidebar:
7
+
order: 8
8
+
description: Connect Hyperdrive to a PlanetScale PostgreSQL database.
9
+
---
10
+
11
+
import { Render } from"~/components";
12
+
13
+
This example shows you how to connect Hyperdrive to a [PlanetScale](https://planetscale.com/) PostgreSQL database.
14
+
15
+
## 1. Allow Hyperdrive access
16
+
17
+
You can connect Hyperdrive to any existing PlanetScale PostgreSQL database by creating a new role (optional) and retrieving a connection string to your database.
18
+
19
+
### PlanetScale Dashboard
20
+
21
+
1. Go to the [**PlanetScale dashboard**](https://app.planetscale.com/) and select the database you wish to connect to.
22
+
2. Click **Connect**.
23
+
3. Create a new role for your Hyperdrive configuration (recommended):
24
+
1. Ensure the minimum required permissions for Hyperdrive to read and write data to your tables:
25
+
-**pg_read_all_data**: Read data from all tables, views, and sequences
26
+
-**pg_write_all_data**: Write data to all tables, views, and sequences
27
+
2. Click **Create role**.
28
+
4. Note the user, the password, the database host, and the database name (or `postgres` as the default database). You will need these to create a database configuration in Hyperdrive.
29
+
30
+
With the host, database name, username and password, you can now create a Hyperdrive database configuration.
When connecting to a PlanetScale PostgreSQL database with Hyperdrive, you should use a driver like [node-postgres (pg)](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/) or [Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/) to connect directly to the underlying database instead of the [PlanetScale serverless driver](https://planetscale.com/docs/tutorials/planetscale-serverless-driver). Hyperdrive is optimized for database access for Workers and will perform global connection pooling and fast query routing by connecting directly to your database.
Copy file name to clipboardExpand all lines: src/content/docs/hyperdrive/reference/supported-databases-and-features.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ Hyperdrive also supports databases that are compatible with the Postgres or MySQ
30
30
| Timescale | ✅ | All | See the [Timescale guide](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/timescale/) to connect. |
31
31
| Materialize | ✅ | All | Postgres-compatible. Refer to the [Materialize guide](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/materialize/) to connect. |
32
32
| CockroachDB | ✅ | All | Postgres-compatible. Refer to the [CockroachDB](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/cockroachdb/) guide to connect. |
33
-
| Planetscale | ✅ | All |Planetscale currently runs MySQL 8.x |
33
+
| Planetscale | ✅ | All |PlanetScale provides MySQL-compatible and PostgreSQL databases|
Copy file name to clipboardExpand all lines: src/content/docs/workers/databases/connecting-to-databases.mdx
+13-14Lines changed: 13 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ D1 is Cloudflare's own SQL-based, serverless database. It is optimized for globa
26
26
Traditional databases use SQL drivers that use [TCP sockets](/workers/runtime-apis/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.
27
27
These drivers are also widely compatible with your preferred ORM libraries and query builders.
28
28
29
-
This also includes serverless databases that are PostgreSQL or MySQL-compatible like [Supabase](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/supabase/), [Neon](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/neon/) or [PlanetScale](/hyperdrive/examples/connect-to-mysql/mysql-database-providers/planetscale/),
29
+
This also includes serverless databases that are PostgreSQL or MySQL-compatible like [Supabase](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/supabase/), [Neon](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/neon/), or PlanetScale (either [MySQL](/hyperdrive/examples/connect-to-mysql/mysql-database-providers/planetscale/) or [PostgreSQL](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/planetscale-postgres/)),
30
30
which can be connected to using both native [TCP sockets and Hyperdrive](/hyperdrive/) or [serverless HTTP-based drivers](/workers/databases/connecting-to-databases/#serverless-databases) (detailed below).
@@ -45,22 +45,21 @@ Since a connection must be re-established on every Worker invocation, this adds
45
45
46
46
### Serverless databases
47
47
48
-
Serverless databases may provide direct connection to the underlying database, or provide HTTP-based proxies and drivers (also known as serverless drivers).
48
+
Serverless databases may provide direct connection to the underlying database, or provide HTTP-based proxies and drivers (also known as serverless drivers).
49
49
50
-
For PostgreSQL and MySQL serverless databases, you can connect to the underlying database directly using the native database drivers and ORMs you are familiar with, using Hyperdrive (recommended) to speed up connectivity and pool database connections. When you use Hyperdrive, your connection pool is managed across all of Cloudflare regions and optimized for usage from Workers.
50
+
For PostgreSQL and MySQL serverless databases, you can connect to the underlying database directly using the native database drivers and ORMs you are familiar with, using Hyperdrive (recommended) to speed up connectivity and pool database connections. When you use Hyperdrive, your connection pool is managed across all of Cloudflare regions and optimized for usage from Workers.
51
51
52
-
You can also use serverless driver libraries to connect to the HTTP-based proxies managed by the database provider. These may also provide connection pooling for traditional SQL databases and reduce the amount of roundtrips needed to establish a secure connection, similarly to Hyperdrive.
53
-
54
-
| Database | Library or Driver | Connection Method |
|[PlanetScale](https://planetscale.com/blog/introducing-the-planetscale-serverless-driver-for-javascript)|[Hyperdrive](/hyperdrive/examples/connect-to-mysql/mysql-database-providers/planetscale), [@planetscale/database](https://github.com/planetscale/database-js)|[mysql2](/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql2/) or [mysql](/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql/), or API via client library |
57
-
|[Supabase](https://github.com/supabase/supabase/tree/master/examples/with-cloudflare-workers)|[Hyperdrive](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/supabase/), [@supabase/supabase-js](https://github.com/supabase/supabase-js)|[node-postgres](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/),[Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/), or API via client library |
58
-
|[Prisma](https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-cloudflare-workers)|[prisma](https://github.com/prisma/prisma)| API via client library |
59
-
|[Neon](https://blog.cloudflare.com/neon-postgres-database-from-workers/)|[Hyperdrive](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/neon/), [@neondatabase/serverless](https://neon.tech/blog/serverless-driver-for-postgres/)|[node-postgres](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/),[Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/), or API via client library |
60
-
|[Hasura](https://hasura.io/blog/building-applications-with-cloudflare-workers-and-hasura-graphql-engine/)| API | GraphQL API via fetch() |
61
-
|[Upstash Redis](https://blog.cloudflare.com/cloudflare-workers-database-integration-with-upstash/)|[@upstash/redis](https://github.com/upstash/upstash-redis)| API via client library |
62
-
|[TiDB Cloud](https://docs.pingcap.com/tidbcloud/integrate-tidbcloud-with-cloudflare)|[@tidbcloud/serverless](https://github.com/tidbcloud/serverless-js)| API via client library |
52
+
You can also use serverless driver libraries to connect to the HTTP-based proxies managed by the database provider. These may also provide connection pooling for traditional SQL databases and reduce the amount of roundtrips needed to establish a secure connection, similarly to Hyperdrive.
63
53
54
+
| Database | Library or Driver | Connection Method |
|[PlanetScale](https://planetscale.com/blog/introducing-the-planetscale-serverless-driver-for-javascript)|[Hyperdrive (MySQL)](/hyperdrive/examples/connect-to-mysql/mysql-database-providers/planetscale), [Hyperdrive (PostgreSQL)](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/planetscale-postgres/), [@planetscale/database](https://github.com/planetscale/database-js)|[mysql2](/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql2/), [mysql](/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql/), [node-postgres](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/), [Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/), or API via client library |
57
+
|[Supabase](https://github.com/supabase/supabase/tree/master/examples/with-cloudflare-workers)|[Hyperdrive](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/supabase/), [@supabase/supabase-js](https://github.com/supabase/supabase-js)|[node-postgres](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/),[Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/), or API via client library |
58
+
|[Prisma](https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-cloudflare-workers)|[prisma](https://github.com/prisma/prisma)| API via client library |
59
+
|[Neon](https://blog.cloudflare.com/neon-postgres-database-from-workers/)|[Hyperdrive](/hyperdrive/examples/connect-to-postgres/postgres-database-providers/neon/), [@neondatabase/serverless](https://neon.tech/blog/serverless-driver-for-postgres/)|[node-postgres](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/),[Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/), or API via client library |
60
+
|[Hasura](https://hasura.io/blog/building-applications-with-cloudflare-workers-and-hasura-graphql-engine/)| API | GraphQL API via fetch() |
61
+
|[Upstash Redis](https://blog.cloudflare.com/cloudflare-workers-database-integration-with-upstash/)|[@upstash/redis](https://github.com/upstash/upstash-redis)| API via client library |
62
+
|[TiDB Cloud](https://docs.pingcap.com/tidbcloud/integrate-tidbcloud-with-cloudflare)|[@tidbcloud/serverless](https://github.com/tidbcloud/serverless-js)| API via client library |
64
63
65
64
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.
[PlanetScale](https://planetscale.com/) is a MySQL-compatible platform that makes databases infinitely scalable, easier and safer to manage.
8
+
[PlanetScale](https://planetscale.com/) is a database platform that provides MySQL-compatible and PostgreSQL databases, making them more scalable, easier and safer to manage.
0 commit comments