Numberedheadings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Automating SQL TasksWriting scripts in PSQL consoleThe course has focused on carrying out database tasks through pgAdmin and the combined SQL window. However full database functionality is available through various command line shell applications, where an experienced database administrator 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 Tools menu. It is also in the bin folder of the postgreSQL install e.g. C:\Program Files\PostgreSQL\13\bin\pgsql.exe (this then can be called via .bat files) When running pgsql from a dos prompt it is useful to 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-commandsThe use of unquoted backslash in Basics
Input / Outputs
Information
You can also execute a command using the -c option e.g. You can execute a script using the -f option Visit the PostgreSQL documentation http://www.postgresql.org/docs/13/static/app-psql.html for a more in depth guide to PSQL. TransactionsDatabase 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. Transaction 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 are produced so other uses cannot modify the table. FunctionsPostgreSQL 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 FunctionsYou can write functions in python (and many other languages), first create the The lines above the first First we import a module ( The PL/Python extension also provides a python module TriggersA 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 triggerA 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 : SyntaxParameters
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:
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.
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 write 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 PythonTo use PostGIS from a Python application you need the Psycopg adapter so you can access PostgreSQL from Python. PsycopgPsycopg 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
Psycopg 2 is both Unicode and Python 3 friendly. On Windows machines use 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:
Simple example using ShapelyShapely does manipulating and analyzing data. It’d It’s based on GEOS, the libraries used by PostGIS. With Shapely, you can do things like buffers, unions, intersections, centroids, convex 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 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. |
...