Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Numberedheadings
number-formatdecimal
skip-headingsHNaN
start-numbering-with10
h1[h1.decimal].
h2[h1.decimal].[h2.upper-latin]
h3
h4
h5
enabledtrue
h6
start-numbering-atH1

Automating SQL Tasks 

Running scripts in the PSQL consolepsql

The course has focused on carrying out database tasks in pgAdmin with SQL, and in QGIS. However full database functionality is also available through command line shell applications, where an experienced database user has full and better control over the database. Included in pgAdmin is the command line application PSQL console which can be found under the Tools menu Tools > PSQL Tool. In a normal PostgreSQL installatiion installation it is in the bin folder (e.g. C:\Program Files\PostgreSQL\13\bin\pgsql.exe), and can be called from a batch file.When running .

Full documentation on psql commands can be found here.

Using psql from pgAdmin means that your database connection is already established. If you run pgsql from a dos prompt it is useful to command prompt, you can set the environment variables first.

Alternatively you will need to connect using the following command:

You will however get promoted for a password.

PSQL MeTA-commands

The use of unquoted backslash in psql is known as a meta-command that is processed by PSQL itself. Meta-commands make PSQL more useful for the database connection to avoid having use the parameters each time - for example:

Alternatively, connect as in the following example (you will be prompted for a password):

Running SQL in psql

To run an SQL command, use the -c option e.g.:
psql -c "select count(*) from geometry_columns"

To execute a script, use the -f option:
psql -f script/vml_count_features.sql

psql Meta-commands

psql includes a set of meta-commands, prefixed with a backslash, that are processed by psql itself. Meta-commands make psql useful for administration or scripting.

Basics

\h

get help with SQL

\?

get help with PSQL

\g OR ;

execute a query

\q

quit

\cd

change directory

Input / Outputs

\echo [STRING]

write string to standard output

\i [FILE]

execute commands from file

\o [FILE]

send all query results to file

 Information

\d [OBJECTNAME]

describes tables, sequences views etc.

\db

lists tablespaces

\df

lists functions

\dn

lists schemas

\dp OR \z

lists tables, views sequences etc.

\du

lists users

\l

lists all databases

\dt

lists tables

You can also execute a command using the -c option e.g.
psql -c "select count(*) from geometry_columns"

You can execute a script using the -f option
psql -f script/vml_count_features.sql

Visit the PostgreSQL documentation for a more in depth guide to PSQL.

Transactions

Database administration often involves providing a coordinated set of commands to the database. An important strength to PostgreSQL is the transaction system, which is where most actions can be executed within a transaction. This allows the administrator to build a script that will either all succeed or all fail, which can be critically important on a production system.

A transaction wraps up a string of commands. Use the word BEGIN to start a transaction and then at the end of transaction include the COMMIT; to complete the transaction.

The transaction will only succeed if all four commands succeed.

To test a transaction before committing is to use ROLLBACK. Rollback enables you to run the transaction to see if it is successful but without making and changes to the database. Rollback will return the data to its original state.

The transaction may succeed but no changes are made. The whole script will fail if at any point, one of the commands gives an error or higher message. While a transaction is in operation it has hold of table locks so other uses cannot modify the table.


Functions

PostgreSQL functions, also known as Stored Procedures, allow you to carry out operations that would normally take several queries and round trips in a single function within the database. Functions allow database reuse as other applications can interact directly with your stored procedures instead of a middle-tier or duplicating code.

There are a large number of in-build function in PostgreSQL and PostGIS. You will find them in the public schema.

Functions can be created in the language of your choice like SQL, PL/pgSQL, C, Python, etc.

N.B. PL/Python is only available as an "untrusted" language, meaning it does not offer any way of restricting what users can do in it and is therefore named plpythonu.

The most common language for creating function is PL/pgSQL

Here is the basic syntax of a function:

Here is a very simple example:

Although function names do not need to be schema qualified, it is recommended that they are placed in a schema. If no schema is specified they will be created in the public schema.

To run the above function you would simply call the function via a select clause. e.g.

For more details on function see https://www.postgresql.org/docs/9.4/static/sql-createfunction.html

For more details on PL/pgSQL see https://www.postgresql.org/docs/9.4/static/plpgsql.html

Python Functions

You can write functions in python (and many other languages), first create the plpython3u extension using CREATE EXTENSION IF NOT EXISTS plpython3u. Then you can create a function using code like:

The lines above the first $$ define the function and it's input and output parameters, then the code between the two $$ marks is a string containing the python code.

First we import a module (xlrd) that handles reading .xls files from Excel. Then we open the workbook contained in the file who's name was passed in, and find the first sheet in the workbook. Finally, we loop through the rows (after skipping row 0 - the header) and return (or yield) the value of the first three cells in the current row. Using yield rather than return means our function can keep track of which row it's on.

The PL/Python extension also provides a python module plpy that provides access to the database. This allows you to query the catalog tables and run {{UPDATE}}s on tables based on the answers.

Triggers

A trigger is a set of actions that are run automatically when a specified change operation (SQL INSERT, UPDATE, DELETE or TRUNCATE statement) is performed on a specified table. Triggers are useful for tasks such as enforcing business rules, validating input data, and keeping an audit trail.

Create trigger

A trigger is a named database object that is associated with a table, and it activates when a particular event (e.g. an insert, update or delete) occurs for the table/views. The statement CREATE TRIGGER creates a new trigger in PostgreSQL. Here is the syntax :

Syntax

Parameters

Name

Description

name

The name of the trigger. A trigger must be distinct from the name of any other trigger for the same table. The name cannot be schema-qualified — the trigger inherits the schema of its table. 

BEFORE 
AFTER
INSTEAD OF

Determines whether the function is called before, after, or instead of the event. A constraint trigger can only be specified as AFTER.

event

One of INSERT, UPDATE, DELETE, or TRUNCATE, that will fire the trigger.

table_name

The name of the table or view the trigger is for.

referenced_table_name

The (possibly schema-qualified) name of another table referenced by the constraint. This option is used for foreign-key constraints and is not recommended for general use. This can only be specified for constraint triggers.

DEFERRABLE NOT 
DEFERRABLE 
INITIALLY IMMEDIATE 
INITIALLY DEFERRED

The default timing of the trigger.

FOR EACH ROW 
FOR EACH STATEMENT

Specifies whether the trigger procedure should be fired once for every row affected by the trigger event, or just once per SQL statement. If neither is specified, FOR EACH STATEMENT is the default.

condition

A Boolean expression that determines whether the trigger function will actually be executed.

function_name

A user-supplied function that is declared as taking no arguments and returning type trigger, which is executed when the trigger fires.

arguments

An optional comma-separated list of arguments to be provided to the function when the trigger is executed. The arguments are literal string constants.

Triggers that are specified to fire INSTEAD OF the trigger event must be marked FOR EACH ROW, and can only be defined on views. BEFORE and AFTER triggers on a view must be marked as FOR EACH STATEMENT. In addition, triggers may be defined to fire for TRUNCATE, though only FOR EACH STATEMENT. The following table summarizes which types of triggers may be used on tables and views:

When

Event

Row-level

Statement-level

BEFORE

INSERT/UPDATE/DELETE

Tables

Tables and views

TRUNCATE

Tables

AFTER

INSERT/UPDATE/DELETE

Tables

Tables and views

TRUNCATE

Tables

INSTEAD OF

INSERT/UPDATE/DELETE

Views

TRUNCATE

Here is a simple example of trigger function.:

Now we can create the trigger which will fire at the time of execution; the event as specified in the trigger for the associated tables.

In the above trigger function there is new keyword 'NEW' which is a PostgreSQL extension to triggers. There are two PostgreSQL extensions to trigger 'OLD' and 'NEW'. OLD and NEW are not case sensitive.

  • Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger

  • In an INSERT trigger, only NEW.col_name can be used.

  • In a UPDATE trigger, you can use OLD.col_name to refer to the columns of a row before it is updated and NEW.col_name to refer to the columns of the row after it is updated.

  • In a DELETE trigger, only OLD.col_name can be used; there is no new row.

A column named with OLD is read only. You can refer to it (if you have the SELECT privilege), but not modify it. You can refer to a column named with NEW if you have the SELECT privilege for it. In a BEFORE trigger, you can also change its value with SET NEW.col_name = value if you have the UPDATE privilege for it. This means you can use a trigger to modify the values to be inserted into a new row or used to update a row. (Such a SET statement has no effect in an AFTER trigger because the row change will have already occurred.)


Here is another example of a trigger, which writes to an audit table.

For more information on triggers and trigger functions see https://www.postgresql.org/docs/9.4/static/plpgsql-trigger.html


Python

To use PostGIS from a Python application you need the Psycopg adapter so you can access PostgreSQL from Python.

Psycopg

Psycopg is the most popular PostgreSQL database adapter for the Python programming language. Its main features are the complete implementation of the Python DB API 2.0 specification and the thread safety (several threads can share the same connection). It was designed for heavily multi-threaded applications that create and destroy lots of cursors and make a large number of concurrent INSERTs or UPDATEs.

Psycopg 2 is mostly implemented in C as a libpq wrapper, resulting in being both efficient and secure. It features client-side and server-side cursors, asynchronous communication and notificationsCOPY TO/COPY FROM support. Many Python types are supported out-of-the-box and adapted to matching PostgreSQL data types; adaptation can be extended and customized thanks to a flexible objects adaptation system.

Psycopg 2 is both Unicode and Python 3 friendly.

On Windows machines use the following to install psycopg2.

Very simple examples of using Psycopg2


What about geometry?

For a simply point you can easily use the appropriate PostGIS functions e.g.

For more complicated geometries, such as LineString and Polygon geometries, you can handle them with a number of tools including

Useful references:

Python Geospatial Development, Erik Westra

Geoprocessing with Python, Chris Garrard


Simple example using Shapely

Shapely does manipulating and analyzing data. It’s based on GEOS, the libraries used by PostGIS. With Shapely, you can do things like buffers, unions, intersections, centroidsconvex hulls.

Shapely, then passes them through psycopg2 as hex-encoded WKB. Note that Shapely 1.3 or later is required to handle the export of 3D geometries with the wkb_hex property.

Of course this can be accomplished by sending the geometry's WKT, however since it is converted to text, it is lossy and may reduce angstroms of precision. Transferring geometries as hex-encoded WKB is lossless, and preserves the exact precision of each coordinate.

...