Numberedheadings | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||||||||||||||||||
Automating SQL TasksThere will be times when you need a task in SQL to be automated, so that it can be triggered from another application, or scheduled to run at a specific time. This module describes a few options for doing this. psqlFull database functionality is available through the psql command line shell application. This can be accessed from within pgAdmin, or on the command line - it is installed auomatically with Postgres, normally in the bin folder (e.g. C:\Program Files\PostgreSQL\13\bin\pgsql.exe), and so can be called from a batch file. Full documentation on psql commands can be found here. psql in pgAdmin
Run pqsl from the Command PromptYou can run pgsql directly from a command prompt (in AppStream, use the OSGeo4W Shell command from the menu).
If you are working from the command line, and always using the same database, set the environment variables for the connection to avoid having use the parameters each time - for example: psql Meta-commandsAs well as being able to run SQL, 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
Input / Outputs
Information
Visit the PostgreSQL documentation for a more in depth guide to PSQL. FunctionsPostgreSQL functions, also known as Stored Procedures, allow you to package queries so that they can be called when needed by any database user, with parameters if appropriate. Postgres/PostGIS are installed with a large number of built in functions - you will find them in the public schema. Functions can be created in the language of your choice , including SQL, PL/pgSQL, C, and Python - the most common language for creating function is PL/pgSQL. The basic syntax of a function is below:Here is
For more details on function functions see https://www.postgresql.org/docs/9.4/static/sql-createfunction.htmlFor more , and for 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.
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 : SyntaxParameters | ||||||||||||||||||||||||||||||||
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 | 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 | The default timing of the trigger. | |||||||||||||||||||||||||||||||
FOR EACH ROW | 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. | |||||||||||||||||||||||||||||||
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 | — | — | ||||||||||||||||||||||||||||||
Python Geospatial Development, Erik Westra | Geoprocessing with Python, Chris Garrard |
Page Comparison
General
Content
Integrations