plsh
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
plsh | 1.20220917 | LANG | MIT | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3080 | plsh | No | Yes | No | Yes | No | Yes | - |
| Related | plpgsql pg_cron pg_task pg_tle plperl plperlu plpython3u plv8 |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 1.20220917 | 1817161514 | plsh | - |
| RPM | PGDG | 1.20220917 | 1817161514 | plsh_$v | - |
| DEB | PGDG | 1.20220917 | 1817161514 | postgresql-$v-plsh | - |
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 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:
| Variable | Description |
|---|---|
PLSH_TG_NAME | Trigger name |
PLSH_TG_WHEN | BEFORE, INSTEAD OF, or AFTER |
PLSH_TG_LEVEL | ROW or STATEMENT |
PLSH_TG_OP | DELETE, INSERT, UPDATE, or TRUNCATE |
PLSH_TG_TABLE_NAME | Target table name |
PLSH_TG_TABLE_SCHEMA | Target 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.
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.