How to autonumber a field in QGIS

To autonumber a field to give it a unique number, is something very common in databases like for example PostgreSQL with a Serial field type. But for other kinds of data sources this too can be very handy. Below I give an example of how you can do this in QGIS. It is a way to autonumber in QGIS that can be used for all kinds of vector data sources, so it’s also usable for for example shapefiles.

This example is split up in two parts. In the first part I explain how you can add a unique number to existing features. In the second part of this example I explain how to autonumber new features.

Add a unique number to existing features

If you have a vector layer that already contains some features (points, polylines or polygons), the first step is to add a field with a unique number for each existing feature.

We’ll do that with the Field Calculator.

  • If you open the Field Calculator for your layer, first check the “Create a new field” checkbox.
  • Then give your new field a name. For this example we will give it the name “nummering”.
  • Make sure the “Output field type” is set to “Whole number (integer)”.
  • Then add @row_number to the Expression box.
  • Click OK

Autonumber new features

In this part we’ll automatically add a unique number to a field if a feature is added to your layer. For this part of the example we’ll assume that your layer has a field of the type intger that will contain our unique numbers. This can be achieved by following the first part of this example or by just adding an extra integer field to your layer. For this example we’ll assume that the field for the unique numbers is called “nummering”.

  • Open the “Properties” of your layer and go to the tab “Attributes Form”.
  • In “Available Widgets” select the field you want to add autonumber to. In this example that is “nummering”.
  • Uncheck the checkbox “Editable” in “General”.
  • In the box “Default value” write: maximum(“nummering”)+1. (An improved formula can be found at the end of this article). Here “nummering” is the name of the field you want to add autonumber to.
  • Click OK

What this does, is that first we check what the current highest number is in the field “nummering” and then new features will get a value that is 1 higher than that maximum.

Now if you add new features to your layer, they automatically get a unique number in the field “nummering”.

IMPORTANT: This trick only works if you manually digitize features. If you use Copy-Paste, the field “nummering” will be empty.

EDIT (Feb. 22, 2021): A better formula that can also be used on new, empty layers is:


This wil use 1 as default value if there are no items in the layer.

It is also important to check the box “Apply default value on update”.

11 Replies to “How to autonumber a field in QGIS”

  1. For Autonumber new features this only appears to work if you already have a value in nummering. How do you do this if you are starting with a completely new dataset with no geometries added yet?

        1. Hi Willy, the latest formula is shown at the bottom of the above blog post. You have to set it as the default value of the field you want to number as is shown in this blog post.

  2. Hi, what if I have created 1 to 10 records and I delete records 3 and 8 and when I create new records it should find the missing value from our range that is from 1 to 10 assign the values 3 and 8 to the newly created records and then continue on with 11, 12, 13…and so on.

    1. Hi Vishnu, that’s not what this solution does. It just checks the current maximum value and for a new record it gives it a value that is 1 bigger than that maximum. Finding missing values would make it much more complex.

    1. Hi David, it only gives a value to new items, because it only looks for a default value if you add a map feature. It doesn’t change anything in existing map features. The “Apply default value on update” setting might sound that it would do that, but it doesn’t.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.