Bootstrap your Cloudflare Worker to connect to a pgEdge database.
This is a simple Cloudflare Worker that connects to a pgEdge database. Use it as a starting point for your own project! Simply click on the button above to deploy this worker to your Cloudflare account and create a Github repository for it.
The easiest way to get your worker connected to a pgEdge database is to go to the pgEdge console. If you have not yet signed up for pgEdge, you can easily sign in with your social accounts. Creating a new database is free and takes less than a minute.
Once you've connected the console, navigate to the database you want to connect to. On the sidebar, click on the "Integrations" tab. You should see a "Cloudflare Workers" integration. This will provide you with all the details you need to connect your worker to your database.
If you prefer to follow READMEs instead of clicking around, here are the steps to get your worker connected...
Once you've deployed your worker and cloned your repository, you'll need to setup your local environment to be able to run the worker locally. You'll need to install wrangler, the Cloudflare CLI tool.
The simplest way to install wrangler
is to use npm
to install dependencies:
npm install
Note that wrangler
requires a Node version of 16.17.0
or later.
The database connection is configured using the DB
environment variable. The value should be in
connection string format, e.g. postgres://user:pass@host/dbname?sslmode=required
. This value
can be added to Cloudflare using one of the npm
scripts like so:
npm run add-db-secret
The value to enter is the connection string from the pgEdge console. This can be found on the Integrations tab of your database under the Cloudflare Workers integration, or on the database details page under the "Connect to your database" section. The "Nearest Node" tab will provide you with the connection string.
Additionally, you should create a .dev.vars
file and set the DB
environment variable there as well
so that you can run the worker locally.
DB=postgres://user:pass@host/dbname?sslmode=required
You can run your worker locally using an npm
script:
npm run dev
Or you can use the Cloudflare dashboard to navigate to your worker and find the deployed worker's URL.