PostGIS Viewer for pgAdmin 3

A couple of months ago, Ivan Mincik released a PostGIS viewer for pgAdmin using the Python version of QGIS libraries. I have taken the source code to add some functionality: now, every new layer is added to the same application and the scale and cursor coordinates are displayed.


Find a newer version here.

PostGIS Viewer for pgAdmin3 (Ubuntu/Linux)

Ivan Mincik has created a pgAdmin plugin that works as a command-line PostGIS viewer as well. The viewer could be useful for spatial databases administrators using pgAdmin.

It is not intended to add major functionalities to the viewer, but if it is required, it could be extended with all the power of Python and QGIS libraries. The PostGIS Viewer allows to display spatial tables and views from  spatial databases.


PREREQUISITES

Python, PyQt4, PyQGIS and libqt4-sql-psql.

How to install prerequisites in Windows: See this post.
How to install prerequisites in Ubuntu/Linux: For QGIS, see downloads official page. For the other programs use apt-get or Synaptic.

DOWNLOADS

For Ubuntu/Linux download the postgis_viewer.py file.

For Windows download postgis_viewer.py and postgis_viewer.bat

HOW TO USE THE VIEWER FROM A COMMAND LINE

Ubuntu/Linux: Set the execution permission:

 $ sudo chmod 755 postgis_viewer.py 

Run the Python file setting some parameters:

 $ postgis_viewer.py -h localhost -U postgres -W secreto -s public -t pais -d geodb & 

If you run the command once more with another table, it will be loaded to the same application:

 $ postgis_viewer.py -h localhost -U postgres -W secreto -s public -t rios -d geodb  

Windows:

Be sure you have the QGIS libraries enabled and run in a command line:

 $ c:\Python25\python.exe postgis_viewer.py -h localhost -U postgres -W secreto -s public -t pais -d geodb &  

INSTALLATION AS A pgAdmin PLUGIN (PostgreSQL 8.4)

Ubuntu/Linux:
Copy the postgis_viewer.py file into one of the folders of the environment variable PATH, for instance, /usr/bin/

 $ sudo cp postgis_viewer.py /usr/bin/  

Set the execution permission:

 $ sudo chmod 755 /usr/bin/postgis_viewer.py  

Copy the next lines to /usr/share/pgadmin3/plugins.ini (if the file does not exist, you have to create it):

  Title=View PostGIS layer
Command=postgis_viewer.py -h $$HOSTNAME -p $$PORT -U $$USERNAME -W $$PASSWORD -d $$DATABASE -s $$SCHEMA -t $$OBJECTNAME
Description=View PostGIS layer
Platform=unix
ServerType=postgresql
Database=Yes
SetPassword=Yes

Windows:

Copy the postgis_viewer.py and postgis_viewer.bat files to C:/Archivos de programa/PostgreSQL/8.4/bin/

Edit C:/Archivos de programa/PostgreSQL/8.4/pgAdmin III/plugins.ini adding the following lines:

  Title=View PostGIS layer
Command="$$PGBINDIR\postgis_viewer.bat" -h "$$HOSTNAME" -p "$$PORT" -U "$$USERNAME" -W "$$PASSWORD" -d "$$DATABASE" -s "$$SCHEMA" -t "$$OBJECTNAME"
Description=View PostGIS layer
KeyFile=$$PGBINDIR\postgis_viewer.bat
Platform=windows
ServerType=postgresql
Database=Yes
SetPassword=Yes

The postgis_viewer.bat file sets the OSGeo4W environment variables (Qt, Python, QGIS) to allow the execution of postgis_viewer.py The .bat file assumes OSGeo4W is installed in C:/ and PostgreSQL can be found in C:/Archivos de programa/PostgreSQL/8.4/ You should check that file if you get some troubles while running the plugin.

PostGIS Viewer for pgAdmin3 (Windows)

NOTE:  The steps are based on PostgreSQL 8.4 and pgAdmin 1.10.

REFERENCES