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”.

3 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?

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.