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

# Database Operations

> Running PostgreSQL queries and Redis commands in Replay workflows.

# Database Operations

The `db` step type executes PostgreSQL queries or Redis commands directly within your workflow.

## PostgreSQL

### Configuration

Set the connection string in workflow config:

````yaml theme={null}
config:
  postgres:
    dsn: postgres://user:password@localhost:5432/mydb

### Running Queries

```yaml
- name: query-users
  type: db
  query: "SELECT id, email, created_at FROM users WHERE status = 'active'"
````

### INSERT with RETURNING

```yaml theme={null}
- name: create-user
  type: db
  query: |
    INSERT INTO users (name, email)
    VALUES ('Alice', 'alice@example.com')
    RETURNING id;
  extract:
    new_id: $[0].id
```

### Extraction from Results

Query results are returned as an array of row objects:

```json theme={null}
[
  { "id": 1, "name": "Alice", "email": "alice@example.com" },
  { "id": 2, "name": "Bob", "email": "bob@example.com" }
]
```

Use JSONPath to extract values:

```yaml theme={null}
extract:
  first_name: $[0].name
  all_emails: $[*].email
```

### Template Variables in Queries

```yaml theme={null}
- name: find-user
  type: db
  query: "SELECT * FROM users WHERE id = {{ user_id }}"
```

### Assertions on Results

```yaml theme={null}
- name: check-user
  type: db
  query: "SELECT count(*) as cnt FROM users"
  assert:
    - ["$[0].cnt", "gt", 0]
```

## Redis

### Configuration

```yaml theme={null}
config:
  redis:
    addr: localhost:6379
```

### Commands

Use the `command` array format:

```yaml theme={null}
- name: set-value
  type: db
  engine: redis
  command: ["SET", "session:1", "active"]
```

### GET and Extract

```yaml theme={null}
- name: get-value
  type: db
  engine: redis
  command: ["GET", "session:1"]
  extract:
    session_status: $
  assert:
    - ["$", "eq", "active"]
```

### Complex Commands

```yaml theme={null}
- name: hash-operations
  type: db
  engine: redis
  command: ["HSET", "user:42", "name", "Alice", "score", "100"]

- name: get-hash
  type: db
  engine: redis
  command: ["HGETALL", "user:42"]
  extract:
    user_data: $
```

## Engine Shorthand

You can use flat shortcuts instead of the `db:` block:

```yaml theme={null}
# Full block
- name: query
  type: db
  db:
    engine: postgres
    query: SELECT 1

# Shorthand
- name: query
  type: db
  engine: postgres
  query: SELECT 1

# Redis shorthand
- name: cache
  type: db
  engine: redis
  command: ["SET", "key", "val"]
```

## What's Next?

* Branch with [control flow](/replay/guides/control-flow) — if/then/else conditions
* Compose workflows with [workflow chaining](/replay/guides/workflow-chaining)
