plsh

PL/sh procedural language

Overview

PackageVersionCategoryLicenseLanguage
plsh1.20220917LANGMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
3080plshNoYesNoYesNoYes-
Relatedplpgsql pg_cron pg_task pg_tle plperl plperlu plpython3u plv8

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG1.202209171817161514plsh-
RPMPGDG1.202209171817161514plsh_$v-
DEBPGDG1.202209171817161514postgresql-$v-plsh-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

Install

You can install plsh directly. First, make sure the PGDG repository is added and enabled:

pig repo add pgdg -u          # Add PGDG repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install plsh;          # Install for current active PG version
pig ext install -y plsh -v 18  # PG 18
pig ext install -y plsh -v 17  # PG 17
pig ext install -y plsh -v 16  # PG 16
pig ext install -y plsh -v 15  # PG 15
pig ext install -y plsh -v 14  # PG 14
dnf install -y plsh_18       # PG 18
dnf install -y plsh_17       # PG 17
dnf install -y plsh_16       # PG 16
dnf install -y plsh_15       # PG 15
dnf install -y plsh_14       # PG 14
apt install -y postgresql-18-plsh   # PG 18
apt install -y postgresql-17-plsh   # PG 17
apt install -y postgresql-16-plsh   # PG 16
apt install -y postgresql-15-plsh   # PG 15
apt install -y postgresql-14-plsh   # PG 14

Create Extension:

CREATE EXTENSION plsh;

Usage

plsh: PL/sh procedural language

plsh allows writing PostgreSQL functions as shell scripts. The function body must start with a shebang line specifying the interpreter.

CREATE EXTENSION plsh;

Create Shell Functions

CREATE FUNCTION concat(text, text) RETURNS text AS '
#!/bin/sh
echo "$1$2"
' LANGUAGE plsh;

SELECT concat('hello ', 'world');  -- 'hello world'

Argument Passing

Function arguments are passed as positional shell variables ($1, $2, etc.):

CREATE FUNCTION file_line_count(filename text) RETURNS int AS '
#!/bin/sh
wc -l < "$1"
' LANGUAGE plsh;

Return Values

  • Standard output becomes the return value (trailing newlines stripped)
  • Empty output returns NULL
  • Standard error output causes the function to abort with that error message
  • Non-zero exit status triggers an error
CREATE FUNCTION system_uptime() RETURNS text AS '
#!/bin/sh
uptime
' LANGUAGE plsh;

Database Access

Direct SPI access is not available, but psql can be used since libpq environment variables are preconfigured:

CREATE FUNCTION query_db(x int) RETURNS text AS $$
#!/bin/sh
psql -At -c "SELECT name FROM users WHERE id = $1"
$$  LANGUAGE plsh;

Trigger Functions

Trigger context is available via environment variables:

VariableDescription
PLSH_TG_NAMETrigger name
PLSH_TG_WHENBEFORE, INSTEAD OF, or AFTER
PLSH_TG_LEVELROW or STATEMENT
PLSH_TG_OPDELETE, INSERT, UPDATE, or TRUNCATE
PLSH_TG_TABLE_NAMETarget table name
PLSH_TG_TABLE_SCHEMATarget table schema

Event trigger variables: PLSH_TG_EVENT, PLSH_TG_TAG.

Inline Execution

DO E'#!/bin/sh\necho "running shell command"' LANGUAGE plsh;

Security

plsh should not be declared as TRUSTED since shell scripts have full OS-level access under the PostgreSQL user. Only superusers should create plsh functions.


Last Modified 2026-03-12: add pg extension catalog (95749bf)