Chapter 47. PL/Python — Python Procedural Language

Table of Contents

47.1. Python 2 vs. Python 3
47.2. PL/Python Functions
47.3. Data Values
47.3.1. Data Type Mapping
47.3.2. Null, None
47.3.3. Arrays, Lists
47.3.4. Composite Types
47.3.5. Set-Returning Functions
47.4. Sharing Data
47.5. Anonymous Code Blocks
47.6. Trigger Functions
47.7. Database Access
47.7.1. Database Access Functions
47.7.2. Trapping Errors
47.8. Explicit Subtransactions
47.8.1. Subtransaction Context Managers
47.8.2. Older Python Versions
47.9. Transaction Management
47.10. Utility Functions
47.11. Environment Variables

The PL/Python procedural language allows PostgreSQL functions and procedures to be written in the Python language.

To install PL/Python in a particular database, use CREATE EXTENSION plpythonu (but see also Section 47.1).

Tip

If a language is installed into template1, all subsequently created databases will have the language installed automatically.

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. A trusted variant plpython might become available in the future if a secure execution mechanism is developed in Python. The writer of a function in untrusted PL/Python must take care that the function cannot be used to do anything unwanted, since it will be able to do anything that could be done by a user logged in as the database administrator. Only superusers can create functions in untrusted languages such as plpythonu.

Note

Users of source packages must specially enable the build of PL/Python during the installation process. (Refer to the installation instructions for more information.) Users of binary packages might find PL/Python in a separate subpackage.