pg_remote_exec
pg_remote_exec · Executes shell commands from SQL and optionally returns their standard output.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100365 | pg_remote_exec | pg_remote_exec | 1.0 | ADMIN | PostgreSQL | C |
PostgreSQL · 12, 13, 14, 15, 16
Usage
Sources:
pg_remote_exec executes operating-system shell commands from a PostgreSQL backend. Despite its name, commands run on the database server host, not on a separately configured remote host. This is an extremely privileged escape from SQL to the operating system and should normally remain uninstalled.
Core Workflow
Only a superuser or a member of pg_execute_server_program can call the functions. If an audited administrative procedure has no safer alternative, revoke default access and wrap one fixed command rather than accepting user-supplied text:
The extension passes the text to the host shell. SQL parameters do not make shell metacharacters safe, and the command’s external effects are not transactional.
Important Objects
pg_remote_exec(text)calls the C librarysystem()function and returns its raw integer status.pg_remote_exec_fetch(text, boolean)callspopen(), returns standard output one line per row, and either raises or suppresses a nonzero close status according to the boolean.pg_execute_server_programis the predefined PostgreSQL role checked at execution time.
Security and Operational Notes
Granting execution is effectively granting arbitrary commands with the database-server operating-system account. It can read files, alter data outside PostgreSQL, open network connections, exhaust resources, or persist malware. Stderr is not captured by the fetch function, cancellation/resource cleanup is incomplete, and long-running commands occupy a backend. Prefer a separately authenticated job runner with an allowlist, timeouts, resource limits, and an audit log.