How to use the expression based (edit: data defined) labeling with postgis?

I'm wondering, whats the best way to use expression based (edit: data defined) labels with postgis (I'm new to postgis). Just by adding new columns for x and y and so on, or by using a relational table with that columns? Or is there a other way?

EDIT: The image below shows the common way to use data defined labels for shape files: Add a column for x and y position for the label (and/or color, size, etc.) to the attribute table and set the correct field in the layer preferences. Now I would like to know if its possible to store the x and y (etc.) columns into a second relational table (with postgis). Because I would like to use the Data-Defined-Function without modifying the attribute table itself.

enter image description here

  • This is a Carto CSS problem, and depends on the visualization tool, I believe. If you're using something like Tilemill to visualize and symbolize your data, Carto CSS can do this easily. Otherwise, I'm not sure what you mean by labeling PostGIS data... – DPSSpatial Mar 7 '14 at 16:06

I'm presuming you have a PostGIS layer added in QGIS, and you've labeled the layer features using an expression. I'm going further and I'm using some color for the label and even a shadow:

enter image description here

Now, the question is how to relate all this with PostGIS. Well, you may save the style of the layer into the database:

enter image description here

The style includes every custom setting you've defined for the label, together with the built expression.

In your PostGIS database a layer_styles will be automatically created, containing your layer default style:

enter image description here

This way, every time when you'll open the PostGIS layer, the label expression and its style will be automatically retrieved from the layer_styles table.

As you can see, there is no need to define additional columns or other relational table to achieve this.


If you ever need to see the data inside the PostGIS layer style, just pay a visit to the PostGIS layer_styles, identify your layer saved style, and click the styleqml column. To properly read the entire xml style (almost 200 lines), you may select all the lines (Ctrl + A), copy it (Ctrl + C) and then paste it (Ctrl + V) into an empty text file.

enter image description here

As an alternative, you can perform the same thing from QGIS:

enter image description here


If you are looking for a way to express the x and y coordinates for all the point features in a PostGIS table or a shapefile, then you may use do this:

enter image description here

And this is the result:

enter image description here

  • I need the "data defined"-function for example for the x and y coordinates of my label (Layer Properties -> Placement -> Data Defined -> Coordinate...). Its not saved in the style file but thats what I wanted. My question was not clear enough. – MAP Mar 8 '14 at 10:05
  • Thanks. The x and y coordinate are saved in a manually created column and not in the stylefile. That means, I still have to create manually a column for x and y like I did when I was working with shapefiles, right? So there is no advantage in that point to use postgis instead of shapefiles. – MAP Mar 9 '14 at 11:26
  • I have to apologize! Thank you for your patience! I added a screenshot to my initial post and I hope that Im clear enough now. – MAP Mar 10 '14 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.