|
| 1 | +--- |
| 2 | +type: example |
| 3 | +summary: Connect Hyperdrive to a Fly database instance. |
| 4 | +pcx_content_type: example |
| 5 | +title: Connect to Fly |
| 6 | +sidebar: |
| 7 | + order: 1 |
| 8 | +description: Connect Hyperdrive to a Fly database instance. |
| 9 | +--- |
| 10 | + |
| 11 | +import { Render, Steps } from "~/components"; |
| 12 | + |
| 13 | +This example shows you how to connect Hyperdrive to a Fly PostgreSQL database instance. |
| 14 | + |
| 15 | +## 1. Allow Hyperdrive access |
| 16 | + |
| 17 | +You can connect Hyperdrive to any existing Fly database by: |
| 18 | + |
| 19 | +1. Allocating a public IP address to your Fly database instance |
| 20 | +2. Configuring an external service |
| 21 | +3. Deploying the configuration |
| 22 | +4. Obtain the connection string, which is used to connect the database to Hyperdrive. |
| 23 | + |
| 24 | +<Steps> |
| 25 | +1. Run the following command to [allocate a public IP address](https://fly.io/docs/postgres/connecting/connecting-external/#allocate-an-ip-address). |
| 26 | + |
| 27 | + ```txt |
| 28 | + fly ips allocate-v6 --app <pg-app-name> |
| 29 | + ``` |
| 30 | + |
| 31 | + :::note |
| 32 | + Cloudflare recommends using IPv6, but some Internet service providers may not support IPv6. In this case, [you can allocate an IPv4 address](https://fly.io/docs/postgres/connecting/connecting-with-flyctl/). |
| 33 | + ::: |
| 34 | + |
| 35 | +2. [Configure an external service](https://fly.io/docs/postgres/connecting/connecting-external/#configure-an-external-service) by modifying the contents of your `fly.toml` file. Run the following command to download the `fly.toml` file. |
| 36 | + |
| 37 | + ```txt |
| 38 | + fly config save --app <pg-app-name> |
| 39 | + ``` |
| 40 | + |
| 41 | + Then, replace the `services` and `services.ports` section of the file with the following `toml` snippet: |
| 42 | + |
| 43 | + ```toml |
| 44 | + [[services]] |
| 45 | + internal_port = 5432 # Postgres instance |
| 46 | + protocol = "tcp" |
| 47 | + |
| 48 | + [[services.ports]] |
| 49 | + handlers = ["pg_tls"] |
| 50 | + port = 5432 |
| 51 | + ``` |
| 52 | + |
| 53 | +3. [Deploy the new configuration](https://fly.io/docs/postgres/connecting/connecting-external/#deploy-with-the-new-configuration). |
| 54 | +4. [Obtain the connection string](https://fly.io/docs/postgres/connecting/connecting-external/#adapting-the-connection-string), which is in the form of: |
| 55 | + |
| 56 | + ```txt |
| 57 | + postgres://{username}:{password}@{public-hostname}:{port}/{database}?options |
| 58 | + ``` |
| 59 | +</Steps> |
| 60 | + |
| 61 | +## 2. Create a database configuration |
| 62 | + |
| 63 | +<Render file="create-hyperdrive-config" /> |
0 commit comments