Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.
Back to main Wiki page
The GUI as a PostgreSQL clientStarting from version 2.1.0 the GUI has the capability to support connections to external PostgreSQL / PostGIS databases.
This functionality is mainly based on libvirtualpg, that is now a required dependency for the GUI tool. The present tutorial is intended to quickly explain how the PostgreSQL support is implemented by spatialite_gui.
1 - Establishing a Connecting to a PostgreSQL DBMS
|1.a first press the appropriate button from the Toolbar|
|1.b then select the appropriate item from the main Menu|
|1.c and finally select the appropriate item from the context Menu that will appear by clicking the left button of the mouse over the PostreSQL main node from the Table Tree.|
|1.d in all three cases the PostgreSQL connection dialog will then appear.
Just insert the required connection arguments and confirm.
When invalid arguments are given, an error message will be displayed, otherwise the connection will be immediately established.
If you are attempting to connect a remote server, you may find it useful to read the tutorial about SSH tunneling and Port Forwarding
2 - Expanding the PostgreSQL tree
|2.a once a new Postgres connection has been successfully established, the Postgres node on the main Table Tree will contain all Tables and Views defined in the Postgres Database, in the same arrangement adopted by PostgreSQL:
The different database types will be identified by an appropriate icon; Views will be distinguished from Tables, and PostGIS Tables (containing Geometry columns) will be clearly distinguished from ordinary Tables.
Note: all Postgres Tables and Views (namely: VirtualPostgres Tables) will always have a conventional name starting with the vpg_ prefix.
Whenever possible the original Postgres Name will be preserved, otherwise a numeric suffix it will be added, ensuring a unique name .
Note: all database objects in the Postgres Tree are dynamic, and will automatically disappear when the corresponding connection has been closed, or when the current session ends.
|2.b if, for any possible reason (such as on: a system crash, power failure, etc.), the current session fails to correctly perform the expected final cleanup, your SQLite database will remain in a dirty and inconsistent state.
As the side figure shows, when restarting a new session all orphaned VirtualPostgres tables, from any aborted session(s), can still be seen.
If this happens, don't despair: all you need to do is to select the appropriate item from the Postgres context menu and the DB will be immediately be converted into a safe, consistent and clean state.
3 - Metadata of PostgreSQL objects
|3.a you can easily query the metadata for each Postgres object; just select the appropriate item from the context menu.|
|3.b Metadata for a Connection aka Database object will report the initial connection arguments.
Note: you can always connect more than one Postgres Database in the same session; however connecting the same Database more than once is not permitted (a warning message will be shown in this case).
|3.c Metadata for a Schema object aren't really very interesting, since only the name will be shown.|
|3.d Metadata for Table or View object will report all GRANT permissions for the current User.
Note: also in the case of a READ-WRITE enabled connection, all Postgres Tables lacking the permission to execute INSERT, UPDATE or DELETE statements will be treated as READ-ONLY Tables.
The same is true for Tables where no PRIMARY KEY has been defined.
|3.e Metadata for a PostGIS Table objects show all settings of the GEOMETRY column(s), including the GeometryType, the Srid and the CoordinateDimension.|
|3.f all PostGIS Tables will always have a companion Table sharing the same identical name, but with an added '_postgis' suffix.
Note: these are not the original VirtualPostgres Tables which store the Geometries in the original Postgres binary format, but are instead Spatialite's own Spatial Views containing Geometries converted to the Spatialite binary format. The conversion between the two formats are done in the background and thus, for the user, in a compleatly transparent way.
4 - Handling PostgreSQL Tables and Views
|4.a PostgreSQL Tables and Views are considered, as far as possible, as if they were native SQLite objects.
So the usual operations supported on SQLite's own objects will be supported also in the case of PostgreSQL objects.
|4.b the above criteria will apply also to PostGIS Geometries.
Hint: exporting a full Shapefile from a PostGIS Table was never more direct and simple than this.
|4.c be warned: directly querying a PostGIS Table (i.e. a VirtualPostgres Table containing PostGIS Geometries) never is a good idea. For reasons explained in 4.d, you'll be always asked twice before starting such an extensive (and potentially dangerous) action.|
|4.d the reason for such a behaviour is clearly shown in the side figure.
A VirtualPostgres Table will expose all Geometries as EWKB text strings using an hexadecimal notation; in this case we have simple POINTS, and the EWKB strings are reasonably compact.
But unhappily complex LINESTRING or POLYGON Geometries may easily require several million characters, and this will easily over stress the GUI.
The execution speed of such a query will certainly be very sluggish, and instability problems, caused by excessive memory use, could eventually arise.
Short conclusion: never, ever, attempt to directly query a PostGIS Table !
|4.e To avoid this problem, a companion Spatial View exists for every PostGIS Table, and is specifically intended to support safe and trouble free queries.
Also the Spatial View will take care to silently convert binary Geometries to and from the needed format, simplifying the writing of customized SQL queries using Spatial Functions since the user does not need to do any binary conversions of the Geometries.
|4.f the companion Spatial View will always be a Writable View when the corresponding PostGIS Table is appropriately enabled and when the connection supports READ-WRITE operations.
Note: All appropriate Triggers (supporting INSERT, UPDATE and DELETE operations), for a Writable Spatial View, will be automatically created.
Addendum: special note for Windows users
All previous versions of the GUI binaries released in the past were always based on a single monolithic .exe fully depending on static linkage, not requiring any other DLL at all.|
Unhappily this is no longer possible, since libpq (the PostgreSQL own client library) is only available as a DLL on Windows, and therefore cannot be used to create a corresponding static library.
More precisely: a static library is actually built when compiling PostgreSQL, but on Windows it simply is a stub still requiring to load the DLL at run time.
For this reason, spatialite_gui.exe, which otherwise depends on a static linkage, must make an exception for libpq.dll.
So a reasonable compromise has been adopted in order to mitigate the negative impact of such an odd combination:
How to load LibPG, when it has not been automatically found
|A.1 libpq.dll will be loaded when accessing a PostgreSQL for the first time:
|A.2 the standard File Selection dialog can be used to locate and open the libpq.dll.
Warning: a common pitfall is attempting to load a 32/64 bit DLL to a 64/32 bit GUI.
The code model (32/64 bit) of both the executable and the DLL must the same.
|A.3 if libpq.dll has been successfully loaded a confirmation message will be shown, with the association between the GUI and the DLL permanently registered.
Once successfully loaded, all the following sessions will use this association automatically, making this a one time effort.
Back to main Wiki page