# aerostack db

> Create D1 and Neon databases, run migrations, and pull schemas from the CLI. Supports multi-database projects and migration history.

Manage D1 and Neon Postgres databases — create databases, run migrations, and pull schemas.

## Migrations (D1)

### Create a migration

```bash
aerostack db migrate new <name>
```

Creates a timestamped SQL migration file in your `migrations/` directory.

```bash
aerostack db migrate new add_users_table
# Creates: migrations/0001_add_users_table.sql
```

### Apply migrations

```bash
# Apply locally (default)
aerostack db migrate apply

# Apply to remote staging
aerostack db migrate apply --remote staging

# Apply to remote production
aerostack db migrate apply --remote production
```

### Check pending migrations

```bash
aerostack db migrate apply --dry-run
```

---

## Neon Postgres

### Create a Neon database

```bash
aerostack db neon create <name>
```

Creates a new Neon Postgres database and adds it to your `aerostack.toml`.

```bash
aerostack db neon create my-postgres-db
```

### Postgres migrations

```bash
# Create a Postgres migration
aerostack db migrate new add_users_table --postgres

# Apply Postgres migrations
aerostack db migrate apply --postgres
aerostack db migrate apply --postgres --remote production
```

---

## Pull schema & generate types

```bash
aerostack db pull
```

Alias for `aerostack generate types`. Introspects your database and generates TypeScript interfaces. See [generate types](/cli/generate) for details.

---

## Typical database workflow

```bash
# 1. Create a new migration
aerostack db migrate new add_products_table

# 3. Apply locally and test
aerostack db migrate apply

# 4. Generate TypeScript types
aerostack generate types

# 5. When ready, apply to staging
aerostack db migrate apply --remote staging
```

Always apply migrations to staging before production. Migrations cannot be rolled back automatically — plan your schema changes carefully.
