# Create and manage a database > Create a managed Postgres database in an Orchard project, connect an app to it, size it, and turn on public access. Source: https://docs.orchard.hackclub.com/data/databases/ Orchard runs managed Postgres databases in your cluster, next to the apps that use them. Each database belongs to a [project](/teams/projects/) and runs as a CloudNativePG cluster. CloudNativePG is the Kubernetes operator that handles failover, storage, and backups, so you do not run Postgres by hand. ## Create a database **Dashboard** 1. Open a project. 2. Select **New app**. 3. In **Start from scratch**, select **Postgres**. 4. In **Start from**, keep **Empty**. You can also select **Import from URL** or **Copy of a database**. 5. In **Database name**, enter a name. 6. In **Postgres version**, select a version. 7. In **Storage size**, set the volume size. The default is 1 GiB. 8. In **Resources**, set the CPU, memory, and **Instances**. 9. Select **Create database**. **CLI** ```sh orchard db create --name appdb --pg-major 17 --storage 10Gi --wait ``` To seed the database from another Postgres server, add `--restore-from postgres://...`. ## Connect an app To connect an app in the same project, use [a reference](/deploy/connect-resources/). A reference follows the database through a password rotation or a restore. **Dashboard** 1. Open the app. 2. Go to **Variables**. 3. Add a variable named `DATABASE_URL`. 4. In the value field, select **Use a resource**. 5. Select the database. 6. Select **Save**. **CLI** ```sh orchard deploy bindings add --database appdb ``` ### Get a connection string 1. Open the database. 2. Select **Connect**. 3. Select **Private** to connect from apps in this project, or **Public** to connect from anywhere. 4. Select a format: **URI**, **psql**, **.env**, or **JDBC**. 5. For a private connection, select the instances: **Primary**, **Standbys**, or **Any**. The private host is a cluster-internal address, such as `pg--rw..svc.cluster.local`. Traffic to it stays inside the cluster. Apps in the same project use this address. To print the connection details from the CLI, run `orchard db connection `. To open a `psql` session, run `orchard db psql `, or use the console on the database page. The session runs in a sandboxed client pod in your namespace as the app role, and ends when you quit `psql`. ## Set CPU and memory Set CPU and memory under **Settings** > **Resources**, or with `orchard db resources set `. Changing them restarts the database. Orchard sizes `shared_buffers` and backup compression from the memory limit, and recalculates them when the memory changes. To set `shared_buffers` yourself, refer to [Change Postgres parameters](/data/parameters/#shared-buffers). > **Caution** > > Postgres reserves `shared_buffers` when it starts. If this value is close to the memory limit, connections and backups have no room in the container, and the kernel can kill Postgres. ## Manage a database | Task | Location | Refer to | | ------------------------------------ | ---------------------------------------------- | ------------------------------------------------------- | | Read logs and open a shell | **Overview** | [Logs and shell](/run/logs/#database-logs) | | Take and restore backups | **Backups** | [Back up and restore a database](/data/backups/) | | Read metrics and query performance | **Observe** | [Monitor a database](/data/monitoring/) | | Rename, stop, or delete the database | **Settings** > **General** and **Danger zone** | [Delete a database](#delete-a-database) | | Grow storage | **Settings** > **Resources** | [Grow database storage](/data/storage/) | | Change protection settings | **Settings** > **Protection** | [Protect a database](/data/protection/) | | Upgrade Postgres | **Security** | [Upgrade Postgres](/data/upgrades/) | | Add standbys | **Settings** > **High availability** | [Configure high availability](/data/high-availability/) | | Turn on the pooler | **Settings** > **Connection pooler** | [Use a connection pooler](/data/connection-pooler/) | | Add users | **Settings** > **Users** | [Add database users](/data/users/) | | Change parameters | **Settings** > **Parameters** | [Change Postgres parameters](/data/parameters/) | | Install extensions | **Settings** > **Extensions** | [Install Postgres extensions](/data/extensions/) | ## Public access By default, only apps in the project can connect. Public access assigns a port on the organization's public IP address, so you can connect from your computer or a business intelligence tool. ### Prerequisites - A layer 4 (L4) load balancer in the cluster. - A public IP address. To set one for your organization, go to **Organization settings** > **Database defaults** > **Public database IP**. Otherwise, the organization inherits the [instance default](/operate/finishing-setup/#set-a-default-public-ip). Public access is not available in Cloudflare Tunnel mode, because tunnels carry HTTP only. ### Turn on public access **Dashboard** 1. Open the database. 2. Go to **Settings** > **Public access**. 3. Turn on **Reachable from the internet**. The connection string appears when the load balancer assigns the address. If the section shows an error, refer to [Troubleshoot a public address](/deploy/networking/#troubleshoot-a-public-address). **CLI** ```sh orchard db public enable ``` > **Caution** > > Anyone with the password can connect to a public database. Use a strong password, and turn off public access when you finish. To change the password, run `orchard db rotate-password `. ## Delete a database Deleting a database moves it to **Recently deleted** on the project for 30 days. You can bring it back only if it had at least one completed backup. For the steps, refer to [Recover a deleted database](/data/backups/#recover-a-deleted-database). **Dashboard** 1. Open the database. 2. Go to **Settings** > **Danger zone**. 3. Select **Delete database**. 4. Confirm the deletion. **CLI** ```sh orchard db delete ``` > **Caution** > > Orchard cannot delete a database while an app still references it. Remove the reference first. ## Related resources - [Read replicas](/data/replicas/) - Send read-only traffic to standbys through a separate endpoint. - [Connect an app to a database or app](/deploy/connect-resources/) - Learn how references keep connection strings current. - [`orchard db` reference](/reference/cli/#db) - Review every database command and flag.