Update of "PostgreSQL"
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

Artifact ID: 416c97c81693b6ada3d23b0d20468a3d5112aa8a
Page Name:PostgreSQL
Date: 2018-08-07 18:27:35
Original User: sandro
Parent: d450f803f39f1e243b1ec588926f81ed30ab301a (diff)
Content

Back to main Wiki page



The GUI as a PostgreSQL client

Starting 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 connection 1
1.b then select the appropriate item from the main Menu connection 2
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. connection 3
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



Special note for Windows users

Please read the Addendum about LibPQ late binding.
connection 4

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:
  • Connection aka Database
    • Schema
      • Table or View


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 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.
table tree
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.
orphan tables

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. metadata info
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).
connection metadata
3.c Metadata for a Schema object aren't really very interesting, since only the name will be shown. schema metadata
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.
table metadata
3.e Metadata for a PostGIS Table objects show all settings of the GEOMETRY column(s), including the GeometryType, the Srid and the CoordinateDimension. postgis metadata
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.
postgis wrapper metadata

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.
table operations
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.
geometry operations
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. direct postgis query
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 !
direct postgis query
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.
indirect postgis query
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.
postgis editing

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:
  • the binaries for Windows platforms will be built in such a way that libpq.dll will support what is generally known as late binding aka deferred binding.
  • this way, the main core of the GUI will continue to work without loading libpq.dll.
  • libpq.dll will be only be loaded when needed (i.e. when the user requests a PostgreSQL connection).
  • if the DLL fails to be loaded for any reason, the PostgreSQL support will remain disabled and the GUI will continue unaffected.

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:
  • an automatic attempt will be performed accordingly to the standard platform rules (mostly: search paths).
    Hint: on Windows, placing the DLL in the same folder as the GUI executable, simplifys matters nicely.
  • if libpq.dll cannot be found, then the user is asked to manually load libpq.dll, as shown by the side figure.
libpq not found
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.
libpq open
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.
libpq loaded
Back to main Wiki page