# relation "pets" already exists / table pets already exists

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


**Short answer:** A table (or view, index or sequence, which PostgreSQL calls relations) with that name already exists. Use `CREATE TABLE IF NOT EXISTS`, drop the old one first if you mean to replace it, or choose another name.

## The exact messages

- PostgreSQL: `ERROR: relation "pets" already exists`
- SQLite: `table pets already exists`

## How PostgreSQL prints it

```
ERROR:  relation "pets" already exists
```

## Why it happens

### Running a setup script twice

The second run meets the tables the first one made. `IF NOT EXISTS` makes the script safe to re-run.

### A name already used by something else

In PostgreSQL, an index or view called `pets` blocks a table called `pets` too: they share one namespace per schema.

## The broken query

```sql
CREATE TABLE pets (id INTEGER PRIMARY KEY, name TEXT);
SELECT 1;
```

## The fix

```sql
CREATE TABLE IF NOT EXISTS pets (id INTEGER PRIMARY KEY, name TEXT);
SELECT COUNT(*) AS pets FROM pets;
```

IF NOT EXISTS skips the CREATE when the table is there. Careful: it doesn't check the columns, so an old table with a different shape stays as it was.

| pets |
| --- |
| 20 |
