# Databases > Run PostgreSQL, MySQL, Redis or MongoDB next to your app, connect to it, and back it up. For agents: [llms.txt](https://ployz.dev/llms.txt) indexes the Ployz docs as Markdown. Ployz adds PostgreSQL, MySQL, Redis or MongoDB to your environment in one step, with a volume for its data, a generated password and the variables your app needs to connect. After that, it's an ordinary service you can change like any other. > [!WARNING] > A database lives on one server. Add a [mirror](https://ployz.dev/docs/services/mirror-a-volume) of its volume > to keep a backup on another of your servers, which you can [restore](https://ployz.dev/docs/services/restore-a-volume) or > move the database to. A mirror isn't an offsite backup, so also take your own dumps and keep > them off your servers: see [Back up a database](#back-up-a-database). ## Add a database 1. On your project's canvas, click **Create**. 2. Choose **Database**, then **PostgreSQL**, **MySQL**, **Redis** or **MongoDB**. 3. Its data goes on a 5 GB [volume](https://ployz.dev/docs/services/volumes). For more room, click the volume, open its **Settings**, type a new **Storage limit** and press Enter. After you deploy it, grow it on Ployz Cloud from the volume's panel, or change it with `ployz volume set NAME --size N`. 4. Click **Deploy**. ![Create → Database: PostgreSQL, Redis, MongoDB and MySQL](https://raw.githubusercontent.com/getployz/ployz/main/docs/user/images/database-preset-menu.png) You get a service named after the database. Its connection variables are exported for your other services to reference. They use Railway's names, like `PGHOST` and `PGPASSWORD`, so an app moving from Railway can keep reading them. The password is a [generated value](https://ployz.dev/docs/services/variables#generate-a-value), `${{ secret(32, "a-zA-Z") }}`: 32 random letters, plain, not sealed, so anyone who can see the service's variables, its environment's History or a sync's changes can read it. Each environment makes its own. A [branch](https://ployz.dev/docs/environments/environments) or a [pull request's preview](https://ployz.dev/docs/environments/preview-environments) that copies the database gets a new password, which its new, empty volume sets up with, and its connection variables reference it. A branch that shares production's database reads production's password. To keep a database's password sealed, seal it from its menu in **Variables**, and set it in each new branch yourself. The seal also follows into branches that copied the database before it: unless a branch changed the password itself, its copy's password turns **Not set** once you deploy the seal. Set it in the branch's **Variables** before the branch deploys. A PostgreSQL or MySQL copy that hasn't set up its volume yet can't start without it. | | Version | Service | URL variable | Port | User | | --- | --- | --- | --- | --- | --- | | PostgreSQL | 18 | `postgres` | `DATABASE_URL` | 5432 | `postgres` | | MySQL | 9.4 | `mysql` | `MYSQL_URL` | 3306 | `root` | | Redis | 8.2 | `redis` | `REDIS_URL` | 6379 | `default` | | MongoDB | 8.0 | `mongodb` | `MONGO_URL` | 27017 | `mongo` | PostgreSQL and MySQL start with a database named `ployz`. ## Connect your app 1. Open your app's service, go to **Variables** and click **New Variable**. 2. Enter `DATABASE_URL` as the **Key**, and leave **Sealed** unticked: a sealed value can't hold a reference. 3. Enter `${{ postgres.DATABASE_URL }}` as the **Value** and click **Add**. 4. Click **Deploy**. ![The New Variable dialog suggesting postgres.DATABASE_URL as you type a reference](https://raw.githubusercontent.com/getployz/ployz/main/docs/user/images/variable-reference.png) The URL points at the database's [private address](https://ployz.dev/docs/services/private-networking), like `postgres.internal`, so the connection stays on your servers' private network. Ployz starts the database before your app on every deploy. See [Variables](https://ployz.dev/docs/services/variables) for more on references. ## Connect from your laptop Databases have no public address. To reach one from your computer, forward a port to it. This is CLI-only for now ([install the CLI](https://ployz.dev/docs/cli/overview) first): ```sh # Forward 127.0.0.1:5432 to the database, until you press Ctrl-C ployz service port-forward postgres 5432:5432 # In another terminal: read the password, then connect ployz exec postgres -- printenv POSTGRES_PASSWORD psql -h 127.0.0.1 -U postgres ployz ``` If 5432 is taken on your laptop, pick another local port, like `15432:5432`. ## Back up a database A [mirror](https://ployz.dev/docs/services/mirror-a-volume) keeps a backup of the database's volume on another of your servers. For an offsite backup, dump each database on a schedule, from a computer you trust or a CI job, and store the dumps away from your servers, such as in object storage. A dump kept on the same server is lost with it. Dumps are CLI-only for now. Keep `-T`: it sends the dump to the file unchanged. ```sh # PostgreSQL ployz exec -T postgres -- pg_dump --format=custom > postgres.dump # MySQL ployz exec -T mysql -- sh -c 'mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" --single-transaction "$MYSQL_DATABASE"' > mysql.sql # Redis ployz exec -T redis -- sh -c 'redis-cli -a "$REDIS_PASSWORD" --no-auth-warning --rdb -' > redis.rdb # MongoDB ployz exec -T mongodb -- sh -c 'mongodump --archive -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin' > mongo.archive ``` Restore a PostgreSQL dump the same way, and try it before you need it: ```sh ployz exec -T postgres -- pg_restore --clean --if-exists -d ployz < postgres.dump ``` ## Use a managed database instead To keep your database at a provider such as Amazon RDS, Neon or PlanetScale, skip the preset. Add the provider's connection URL to your app as a sealed variable named `DATABASE_URL`, then click **Deploy**. If the provider only accepts known IP addresses, allow your servers' public IPs. ## Good to know - **The password is set once.** PostgreSQL, MySQL and MongoDB read their password only when they first set up their volume. If you change the password variable later, change it in the database too, or your app can't connect. Redis reads `REDIS_PASSWORD` each time it starts. - **A database added before Ployz 0.3.6 keeps its password as it is.** Its branches and previews copy a plain password rather than making a new one. To give each branch its own, set the password to `${{ secret(32, "a-zA-Z") }}` in the branch before its copy first deploys. - **A database added while Ployz sealed its password keeps it sealed.** Ployz can't read a sealed value, so it never unseals one. Its branches and previews show the password **Not set**, and their copy of the database can't start until you set it in the branch's **Variables**. - **Redis can lose its last minute of writes** if it crashes. - **Deleting a database deletes its data.** Deleting its service in the dashboard also deletes its volume, unless another service mounts it. The deploy asks you to confirm first. Next: [test changes in a preview environment](https://ployz.dev/docs/environments/preview-environments).