---
title: "Attach a database to a project: writes its internal connection string as a secret variable (default DATABASE_URL)"
method: POST
path: "/databases/{databaseId}/attach"
tags: ["Databases"]
---

# Attach a database to a project: writes its internal connection string as a secret variable (default DATABASE_URL)

`POST /databases/{databaseId}/attach`

## Request

- Base URL: `https://hostingsurge.com/api/v1`
- URL: `https://hostingsurge.com/api/v1/databases/{databaseId}/attach`
- Auth: one of:
  - HTTP bearer
  - API key in cookie `__Host-hs_session`

## Path parameters

- `databaseId` string, uuid, required

## Request body

- object
  - `projectId` string, uuid, required
  - `envKey` string
  - `overwrite` boolean: replace an existing variable with the same key (otherwise 409 conflict)
  - `conventions` boolean: also every name the usual frameworks read, like Vercel's integrations (PostgreSQL: POSTGRES_URL, POSTGRES_PRISMA_URL, POSTGRES_URL_NON_POOLING, POSTGRES_HOST/USER/PASSWORD/DATABASE, PG*; MySQL/MariaDB: MYSQL_URL, MYSQL_*; MongoDB: MONGODB_URI, MONGO_URL; Redis: REDIS_URL, KV_URL); overwrite then applies to each of them

## Response `200`

Attached

- object
  - `database` object, required
    - `id` string, uuid, required
    - `organizationId` string, uuid, required
    - `projectId` string, uuid, nullable, required
    - `name` string, required
    - `engine` 'postgresql' | 'mysql' | 'mariadb' | 'redis' | 'mongodb', required
    - `version` string, nullable, required
    - `status` 'provisioning' | 'running' | 'stopped' | 'error' | 'deleting', required
    - `isPublic` boolean, required
    - `internalHost` string, nullable, required: reachable from the organization's projects
    - `port` integer, nullable, required
    - `publicHost` string, nullable, required
    - `publicPort` integer, nullable, required
    - `databaseName` string, nullable, required
    - `username` string, nullable, required
    - `password` string, nullable, required: only with databases:reveal and ?reveal=true
    - `connectionUrl` string, nullable, required: masked unless revealed
    - `backupScheduled` boolean: false: the database has no backup schedule, so nothing is backed up (absent for Redis, which has no scheduled backups). PUT /databases/{id}/backups {"frequency": "0 3 * * *", "retentionDays": 30} turns on daily backups.
    - `resources` object, required: CPU and memory limits of the database container; change them with POST /databases/{id}/resources (restarts it)
      - `cpus` number, required
      - `memoryMb` integer, required
    - `createdAt` string, required: ISO-8601 timestamp
  - `variable` object, required: the written variable (value masked)
    - `id` string, uuid, required
    - `key` string, required
    - `value` string, nullable, required: null when secret and not revealed
    - `secret` boolean, required
    - `targets` string[], required: the environments this entry's value is for; one name can have one entry per environment
    - `gitBranch` string, nullable, required: set: a preview value for this Git branch only
    - `buildtime` boolean, required
    - `runtime` boolean, required
    - `updatedAt` string, required: ISO-8601 timestamp

## Other responses

- `400`: Invalid request (code invalid_request)
- `401`: Missing or invalid session/token (code unauthorized)
- `403`: Authenticated but not permitted (code forbidden)
- `404`: Not found or not visible to the caller (code not_found)
- `409`: Conflicting state (code conflict)
- `429`: Too many requests (code rate_limited); see Retry-After
- `503`: Hosting engine unavailable (code provider_unavailable)

---

[API](https://skmtc.dev/hostingsurge/apis/hostingsurge-platform-api.md) · [All operations](https://skmtc.dev/hostingsurge/apis/hostingsurge-platform-api/llms.txt) · [OpenAPI document](https://skmtc.dev/hostingsurge/apis/hostingsurge-platform-api/revisions/4d08337cdff1?raw)
