Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cloudflare/worker-template-postgres
Reference demo and modified PostgreSQL driver to connect Cloudflare Workers to a relational database.
https://github.com/cloudflare/worker-template-postgres
cloudflare database postgres serverless sql workers
Last synced: about 1 month ago
JSON representation
Reference demo and modified PostgreSQL driver to connect Cloudflare Workers to a relational database.
- Host: GitHub
- URL: https://github.com/cloudflare/worker-template-postgres
- Owner: cloudflare
- License: apache-2.0
- Created: 2021-11-03T19:00:06.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-09-25T03:15:17.000Z (about 2 months ago)
- Last Synced: 2024-10-04T21:41:33.485Z (about 1 month ago)
- Topics: cloudflare, database, postgres, serverless, sql, workers
- Language: JavaScript
- Homepage:
- Size: 359 KB
- Stars: 85
- Watchers: 6
- Forks: 15
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE_APACHE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Cloudflare Workers + PostgreSQL
This repo contains example code and a PostgreSQL driver that can be used in any Workers project. If
you are interested in using the driver _outside_ of this template, copy the `driver/postgres` module
into your project's `node_modules` or directly alongside your source.## Usage
Before you start, please refer to the **[official tutorial](https://developers.cloudflare.com/workers/tutorials/query-postgres-from-workers-using-database-connectors)**.
```typescript
const client = new Client({
user: '',
database: '',
// hostname is the full URL to your pre-created Cloudflare Tunnel, see documentation here:
// https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/create-tunnel
hostname: env.TUNNEL_HOST || 'https://dev.example.com',
password: env.DATABASE_PASSWORD, // use a secret to store passwords
port: '',
})await client.connect()
```**Please Note:**
- you must use this config object format vs. a database connection string
- the `hostname` property must be the URL to your Cloudflare Tunnel, _NOT_ your database host
- your Tunnel will be configured to connect to your database host## Running the Postgres Demo
`postgres/docker-compose.yml`
This docker-compose composition will get you up and running with a local instance of `postgresql`,
`pgbouncer` in front to provide connection pooling, and a copy of `cloudflared` to enable your
applications to securely connect, through a encrypted tunnel, without opening any ports up locally.### Usage
> from within `scripts/postgres`, run:
1. **Create credentials file (first time only)**
```sh
docker run -v ~/.cloudflared:/etc/cloudflared cloudflare/cloudflared:2021.10.5 login
```2. **Start a local dev stack (cloudflared/pgbouncer/postgres)**
```sh
TUNNEL_HOSTNAME=dev.example.com docker-compose up
```