pg_execute-server_program#
What is pg_execute-server_program?#
This is a built-in privileged role provided by PostgreSQL.
pg_execute_server_program allows a role to execute programs on the PostgreSQL serverβs operating system.
Not SQL programs.
Not database functions.
Actual OS-level commands.
Plain English Meaning
- If a user has this role, PostgreSQL can:
Run shell commands
Execute binaries
Interact with the serverβs filesystem
Call external programs
β οΈ This means server-level access, not just database access.
How is it used?
It is mainly used with:
1οΈβ£ COPY β¦ TO/FROM PROGRAM
Example:
COPY mytable TO PROGRAM 'gzip > /tmp/mytable.gz';
Or:
COPY mytable FROM PROGRAM 'curl http://example.com/data.csv';
Only roles with pg_execute_server_program can do this
- 2οΈβ£ Some extensions or admin tooling
Backup automation
Controlled ETL pipelines
Advanced admin tasks (very rare)
Why is this role so restricted?#
Because it is equivalent to OS command execution.
- If misused, a user could:
Read /etc/passwd
Write files to disk
Call curl, wget, rm, etc.
Exfiltrate data
Destroy the server
In cloud environments, this is often blocked entirely.
- π« Default behavior
β Not granted to normal users
β Not granted to application roles
β Not for web apps
β Not for ORMs (Django, SQLAlchemy, etc.)
Only superusers or trusted DBAs should ever have it.
Compare with related roles
Role |
What it allows |
|---|---|
pg_read_server_files |
Read files from server |
pg_write_server_files |
Write files to server |
pg_execute_server_program |
Execute OS commands |
pg_read_all_data |
Read all tables |
pg_write_all_data |
Write all tables |
This is the most dangerous of them all
Example (What NOT to do)
GRANT pg_execute_server_program TO sherullahmohtat;
β Do not do this on production
β Do not do this for apps
β Do not do this unless you fully trust the user
β When is it actually appropriate?
- Only in cases like:
β Dedicated DBA account
β Offline data migration machine
β Controlled internal tooling
β Isolated test environment
β Air-gapped system
Even then β audit everything
π PostgreSQL Security Model Insight#
- PostgreSQL separates:
SQL permissions
Filesystem access
OS execution
pg_execute_server_program breaks the sandbox.
Thatβs why PostgreSQL keeps it explicit and visible in pgAdmin.
Mental Model (Remember this)
Giving pg_execute_server_program is like giving sudo to PostgreSQL.
If PostgreSQL can run it β it runs as the postgres OS user.
β Best Practice (Recommended)
- For most systems:
β Never use pg_execute_server_program
β Use application-level ETL (Python, Spark, Airflow)
β Use COPY TO STDOUT instead
β Use database-safe imports
- Summary
pg_execute_server_program = OS command execution
Extremely powerful
Extremely dangerous
Almost never needed
Should be granted only by experts