# relation "n" does not exist (a recursive CTE without RECURSIVE)

Source: https://sqlstreak.com/sql/errors/with-recursive-missing · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**Short answer:** A CTE that refers to itself must be declared `WITH RECURSIVE`. Without the keyword, PostgreSQL looks for an existing table with that name and doesn't find one.

## The exact messages

- PostgreSQL: `ERROR: relation "n" does not exist`

## How PostgreSQL prints it

```
ERROR:  relation "n" does not exist
LINE 4:   SELECT x + 1 FROM n WHERE x < 5
                            ^
DETAIL:  There is a WITH item named "n", but it cannot be referenced from this part of the query.
HINT:  Use WITH RECURSIVE, or re-order the WITH items to remove forward references.
```

## Why it happens

### RECURSIVE left out

The CTE's second half selects from the CTE itself (`FROM n`), which only works when it's declared recursive.

### A CTE used before it's defined

Non-recursive CTEs can only use the ones written above them. Reorder them, or add RECURSIVE.

## The broken query

```sql
WITH n(x) AS (
  SELECT 1
  UNION ALL
  SELECT x + 1 FROM n WHERE x < 5
)
SELECT x FROM n;
```

## The fix

```sql
WITH RECURSIVE n(x) AS (
  SELECT 1
  UNION ALL
  SELECT x + 1 FROM n WHERE x < 5
)
SELECT x FROM n;
```

One word: RECURSIVE. The query then counts from 1 to 5 in both databases.

| x |
| --- |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |

## What SQLite does instead

SQLite treats the RECURSIVE keyword as optional, so the same query runs there. Write it anyway: it's required in PostgreSQL and makes the intent clear.
