# How to define an auto increment primary key in PostgreSQL

> Learn how to define an auto-increment primary key in PostgreSQL using the SERIAL type with a PRIMARY KEY constraint, and the MySQL AUTO_INCREMENT equivalent.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2022-05-16 | Topics: [Database](https://flaviocopes.com/tags/database/) | Canonical: https://flaviocopes.com/postgres-auto-increment-primary-key/

To define a primary key that auto increments in [PostgreSQL](https://flaviocopes.com/postgres-introduction/) you create the table row using the `SERIAL` type with the `PRIMARY KEY` constraint, like this:

```sql
CREATE TABLE cars (
  id    SERIAL PRIMARY KEY,
  brand VARCHAR(30) NOT NULL,
  model VARCHAR(30) NOT NULL,
  year  CHAR(4) NOT NULL
);
```

In [MySQL](https://flaviocopes.com/mysql-how-to-install/) / MariaDB this is equivalent to 

```sql
CREATE TABLE cars (
  id    INT(11) NOT NULL AUTO_INCREMENT,
  brand VARCHAR(30) NOT NULL,
  model VARCHAR(30) NOT NULL,
  year CHAR(30) NOT NULL,
  PRIMARY KEY (`id`)
);
```
