Auto Increment on PostgreSQL follow-up..

Just a quick follow-up post here regarding how to achieve auto incrementing on a PostgreSQL field.

Thanks to the advise from Senokian I was able to create our database with the exacy same functionality as our MSSQL one. The only field we were concerned with was the customerid one, which should increment by 1 each time a record was added.

An example of this is:

CREATE TABLE customers (customerid bigserial not null);

You can either use serial or bigserial depending on your needs.

And that’s all there is to it. After you issue that command, PostgreSQL will automatically create the sequence for you, and deal with everything else that is going on in the background. If you don’t give the sequence a name, in this case it will/should call it customers_customerid_seq

P.S don’t forget if you want to use your table properly with PHP etc, you need to grant permissions on the table, i.e.:

GRANT INSERT ON customers TO username;