> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microsandbox.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL

> Run a persistent PostgreSQL service with a localhost-only forwarded port

Local-only (This guide publishes a client-host port, which is not available on microsandbox cloud.)

This guide runs PostgreSQL 17 in a microVM, stores the database on a sized root disk, and publishes the service only to host loopback.

> [!NOTE]
>
> The current `postgres:17-alpine` image contains tab characters in `DOCKER_PG_LLVM_DEPS`. Passing `-e DOCKER_PG_LLVM_DEPS=` is a temporary compatibility workaround for microsandbox's guest environment validation.

## Run PostgreSQL

**1. Start PostgreSQL**

Set a demo password in the host shell:

```sh macOS & Linux theme={null}
export POSTGRES_PASSWORD="$(openssl rand -hex 24)"
```

```powershell Windows theme={null}
$bytes = New-Object byte[] 24
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($bytes)
$rng.Dispose()
$env:POSTGRES_PASSWORD = -join ($bytes | ForEach-Object { $_.ToString('x2') })
```

Start the database:

```sh macOS & Linux theme={null}
msb run -d --name postgres-demo --replace \
  --cpus 1 --memory 1G --root-disk 6G \
  -p 127.0.0.1:55432:5432 \
  -e POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \
  -e POSTGRES_DB=examples \
  -e DOCKER_PG_LLVM_DEPS= \
  postgres:17-alpine
```

```powershell Windows theme={null}
msb run -d --name postgres-demo --replace `
  --cpus 1 --memory 1G --root-disk 6G `
  -p 127.0.0.1:55432:5432 `
  -e "POSTGRES_PASSWORD=$env:POSTGRES_PASSWORD" `
  -e POSTGRES_DB=examples `
  -e DOCKER_PG_LLVM_DEPS= `
  postgres:17-alpine
```

With no command after the image, microsandbox runs the image's declared `docker-entrypoint.sh postgres` command in the background.

**2. Wait for readiness**

```sh Shell theme={null}
msb exec postgres-demo -- sh -lc '
  until pg_isready -h 127.0.0.1 -d examples -U postgres; do sleep 1; done
'
```

**3. Run a query**

```sh macOS & Linux theme={null}
msb exec -e PGPASSWORD="$POSTGRES_PASSWORD" postgres-demo -- \
  psql -h 127.0.0.1 -U postgres -d examples \
  -c "select current_database(), current_setting('server_version');"
```

```powershell Windows theme={null}
msb exec -e "PGPASSWORD=$env:POSTGRES_PASSWORD" postgres-demo -- `
  psql -h 127.0.0.1 -U postgres -d examples `
  -c "select current_database(), current_setting('server_version');"
```

The tested image returned database `examples` and PostgreSQL `17.10`.

Applications on the host can connect to `127.0.0.1:55432` with the same database, user, and password.

> [!WARNING]
>
> Environment variables and command arguments are appropriate for this local demo, not for production secrets. Use [secret injection](https://docs.microsandbox.dev/sandboxes/secrets.md), retain the loopback bind, and configure PostgreSQL access controls before allowing remote clients.

**4. Clean up**

Remove the sandbox and its database:

```sh Shell theme={null}
msb rm -f postgres-demo
```

Clear the password from the host shell:

```sh macOS & Linux theme={null}
unset POSTGRES_PASSWORD
```

```powershell Windows theme={null}
Remove-Item Env:POSTGRES_PASSWORD
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.