---
title: "Deploying a Database on OpenShift"
canonical: "https://kb.uconn.edu/space/IKB/28728131665/Deploying%20a%20Database%20on%20OpenShift"
format: markdown
---
Most applications need a database. This guide covers deploying **MySQL** and **PostgreSQL** on OpenShift, connecting your app, and managing backups.

---

## Option 1: Deploy from the Developer Catalog (Web Console)

1. In the OpenShift web console, switch to the **Developer** perspective.
2. Click **+Add** → **Database** from the sidebar.
3. Select your database (e.g., **PostgreSQL** or **MySQL**).
4. Fill in the configuration:
  - **Database Service Name** — e.g., `postgresql`
  - **Database Name** — e.g., `myappdb`
  - **Database User** — e.g., `appuser`
  - **Database Password** — choose a strong password
5. Click **Create**.

OpenShift will automatically create a Deployment, Service, Secret (with credentials), and PVC (for data persistence).

---

## Option 2: Deploy via CLI

### PostgreSQL

```shell
# Deploy PostgreSQL with persistent storage
oc new-app postgresql-persistent \
  --param=POSTGRESQL_USER=appuser \
  --param=POSTGRESQL_PASSWORD=s3cur3P@ss \
  --param=POSTGRESQL_DATABASE=myappdb \
  --param=VOLUME_CAPACITY=5Gi \
  --name=postgresql

# Verify the pod is running
oc get pods -l name=postgresql

# Check the generated secret
oc get secret postgresql -o yaml
```

### MySQL

```shell
# Deploy MySQL with persistent storage
oc new-app mysql-persistent \
  --param=MYSQL_USER=appuser \
  --param=MYSQL_PASSWORD=s3cur3P@ss \
  --param=MYSQL_DATABASE=myappdb \
  --param=VOLUME_CAPACITY=5Gi \
  --name=mysql

# Verify the pod is running
oc get pods -l name=mysql
```

---

## Connecting Your Application to the Database

The database credentials are stored in a Secret. Inject them into your app as environment variables:

```shell
# Link the database secret to your app
oc set env deployment/my-app --from=secret/postgresql

# Or set the connection string manually
oc set env deployment/my-app \
  DATABASE_URL=postgres://appuser:s3cur3P@ss@postgresql.my-project.svc:5432/myappdb
```

### Connection String Formats

| Database | Format |
| --- | --- |
| **PostgreSQL** | `postgres://USER:PASS@SERVICE.PROJECT.svc:5432/DBNAME` |
| **MySQL** | `mysql://USER:PASS@SERVICE.PROJECT.svc:3306/DBNAME` |

> **Important:** Use the internal service DNS name (e.g., `postgresql.my-project.svc`), not a route. Database traffic should stay within the cluster.

---

## Accessing the Database Shell

```shell
# PostgreSQL
oc rsh deployment/postgresql
psql -U appuser myappdb

# MySQL
oc rsh deployment/mysql
mysql -u appuser -p myappdb
```

---

## Backing Up Your Database

### PostgreSQL Backup

```shell
# Create a backup
oc exec deployment/postgresql -- pg_dump -U appuser myappdb > backup.sql

# Restore from backup
oc exec -i deployment/postgresql -- psql -U appuser myappdb < backup.sql
```

### MySQL Backup

```shell
# Create a backup
oc exec deployment/mysql -- mysqldump -u appuser -ps3cur3P@ss myappdb > backup.sql

# Restore from backup
oc exec -i deployment/mysql -- mysql -u appuser -ps3cur3P@ss myappdb < backup.sql
```

> **Best Practice:** Set up a CronJob to automate regular backups. Contact the ITS Platform Team for guidance on backup scheduling.

---

## Storage Considerations

| Factor | Recommendation |
| --- | --- |
| **Access Mode** | Use `ReadWriteOnce (RWO)` — databases need single-node write access |
| **Size** | Start with 5-10Gi; monitor usage and expand as needed |
| **Backup** | Always back up before major changes or upgrades |
| **Performance** | For high-traffic apps, discuss storage class options with the ITS Platform Team |

---

## Troubleshooting Database Issues

| Symptom | Cause | Fix |
| --- | --- | --- |
| Pod stuck in `Pending` | PVC not bound | Check `oc get pvc` — contact ITS if stuck |
| Connection refused from app | Wrong service name or port | Verify with `oc get svc` and check your connection string |
| Permission denied | Wrong credentials | Check the Secret values with `oc get secret postgresql -o yaml` |
| Data lost after restart | No persistent storage | Redeploy with `-persistent` template to use a PVC |
| Database out of space | PVC too small | Expand the PVC or clean up old data |

---

*UConn ITS Infrastructure Services — OpenShift Knowledge Base*