Sorry about the strange title, I didn't really know how to word it.

I currently use MapInfo to do most of my GIS work however I have recently installed PostGIS 1.5 and I really like it so far.

A project that I am going to start working on in the future sometime will involve creating an application for managing stormwater assets, all my previous mapping projects have just been using a MapInfo window embedded in a C# application. Now that I have some data in a PostGIS server it has opened a lot of possibilities.

I tried writing some small sample applications using NHibernate and NHibernate.Spatial in C# but to say it's frustrating is an understatement (sometimes static typed code gives me the shits).

So I was wondering if anyone had any experience writing a QGIS plugin using python to access PostGIS and if you think it would be a viable route to take, and if so anyone had any good places to read about doing such things.

3 Answers 3


There are a lot of python plugins for QGIS that access PostGIS. Just download them and check the source code. Accessing PostGIS from Python is quite straight forward (same is true for SpatiaLite by the way).

PostGIS related plugins by name:

  • PgQuery for QGIS
  • PostGIS Manager
  • PostGIS SQL Query Editor
  • RT Sql Layer
  • ...

I have no experience or knowledge of the QGIS API. What I know is that it is a viable approach and it can be a very rewarding experience.

You can check out PyQGIS Cook Book - that shows a lot of code on how to achieve common taks. That might help you.



from pyQGis cookbook:

uri = QgsDataSourceURI()

#set host name, port , database name, username and password
uri.setConnection("localhost ","5432 " , " dbname"," johny","xxx ")

#set database schema, table name, geometry column and optionaly subset(WHERE clause)
uri.setDataSource ("public ","roads ", "the _geom ", "cityid=2643 ")

vlayer=QgsVectorLayer (uri .uri() ,"layer _ name_you_ like","postgres ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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