Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:57:47+00:00 2026-06-01T01:57:47+00:00

Using Postgres, I’m trying to use AUTO_INCREMENT to number my primary key automatically in

  • 0

Using Postgres, I’m trying to use AUTO_INCREMENT to number my primary key automatically in SQL. However, it gives me an error.

CREATE TABLE Staff   (
  ID        INTEGER NOT NULL AUTO_INCREMENT,
  Name      VARCHAR(40) NOT NULL,
  PRIMARY KEY (ID)
);

The error:

********** Error **********
ERROR: syntax error at or near "AUTO_INCREMENT"
SQL state: 42601
Character: 63

Any idea why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:57:48+00:00Added an answer on June 1, 2026 at 1:57 am

    Postgres 10 or later

    Consider a standard-SQL IDENTITY column. serial columns remain unchanged. (See below.) But the former is preferable in modern Postgres. Can be GENERATED BY DEFAULT or (stricter) GENERATED ALWAYS.
    Basics in the manual for CREATE TABLE.
    Details in this blog entry by its principal author Peter Eisentraut.

    Create table with IDENTITY column

    CREATE TABLE staff (
      staff_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY
    , staff    text NOT NULL
    );

    Add IDENTITY column to existing table

    Table may or may not be populated with rows.

    ALTER TABLE staff ADD COLUMN staff_id int GENERATED ALWAYS AS IDENTITY;
    

    To also make it the PK at the same time (table can’t have a PK yet):

    ALTER TABLE staff ADD COLUMN staff_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY;
    

    See:

    • How to add a PostgreSQL 10 identity column to an existing table with rows?

    Replace serial with IDENTITY column

    See:

    • How to change a table ID from serial to identity?

    You can override system values or user input in INSERT commands with OVERRIDING {SYSTEM|USER} VALUE.

    Postgres 9.6 or older

    (Still supported in newer versions, too.)
    Use the serial pseudo data type:

    CREATE TABLE staff (
      staff_id serial PRIMARY KEY,
    , staff    text NOT NULL
    );

    It creates and attaches the sequence object automatically and sets the DEFAULT to nextval() from the sequence. It does all you need.

    I use legal, lower-case, unquoted identifiers in my examples. Makes your life with Postgres easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using postgres and I'm getting the duplicate key error when updating a row:
Simple rails app using Postgres DB, getting 'integer out of range' error when trying
Using Postgres on Heroku Cedar, when I try to verify a public key using
A tough SQL question (I'm using postgres by the way). I need the first
I am using Postgres 8.4.4 copy, http://www.postgresql.org/docs/8.4/static/sql-copy.html , to import CSV data into my
I have a number of tables that use the Postgres Partitioning feature. I want
I can compile but can't execute the following code with the error (using Postgres):
I'm using Postgres and I'm trying to write a query like this: select count(*)
i am using hibernate annotations, at the back end i am using Postgres SQL
I'm using Postgres, and I have a large number of rows that need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.