Kong Gateway needs different PostgreSQL privileges depending on the operation being performed, following the principle of least privilege.
Both the write user and the read-only user need CONNECT privilege on pg_database. PostgreSQL grants CONNECT to PUBLIC by default. As a result, this privilege is already open for all roles, unless you revoke it.
Applies to:
- Traditional mode with
database = postgres.
- Hybrid mode control plane nodes (
role = control_plane).
Both run kong migrations against a real schema.
Does not apply to:
- Traditional DB-less mode (
database = off), because it has no schema to migrate.
- Hybrid mode data plane nodes (
role = data_plane). Kong Gateway config validation forces database = off for a data plane node. As a result, the node never connects to PostgreSQL.
Before migration, the write user needs CREATE privilege on pg_database to create the Kong Gateway schema.
kong migrations reset/bootstrap/up/finish runs the following Data Definition Language (DDL) statements automatically. The following script makes the write user the schema owner (replace $PG_SCHEMA with the actual name of pg_schema):
-- kong migrations reset
DROP SCHEMA IF EXISTS $PG_SCHEMA CASCADE;
-- kong migrations bootstrap/up/finish
CREATE SCHEMA IF NOT EXISTS $PG_SCHEMA AUTHORIZATION CURRENT_USER;
GRANT ALL ON SCHEMA $PG_SCHEMA TO CURRENT_USER;
SET SCHEMA $PG_SCHEMA;
After migration, the write user owns the schema and every object the migration session creates in it. Ownership already grants the write user every privilege it needs. As a result, no further GRANT is necessary.
Applies to:
- Traditional mode with
database = postgres.
- Hybrid mode control plane nodes (
role = control_plane).
Does not apply to: Modes that don’t open a database connection.
- Traditional DB-less mode loads configuration from a declarative file instead of a database.
- A hybrid mode data plane node keeps all state in memory.
If the same role runs migrations and runtime, skip this section. Ownership already covers it. If you configure a different write user for runtime, grant it the following privileges:
|
Privilege
|
On
|
Purpose
|
USAGE
|
pg_schema schema
|
Access the Kong Gateway schema.
|
SELECT, INSERT, UPDATE, DELETE
|
All tables in the schema
|
Admin API CRUD operations, audit logging, and plugin DAO writes.
|
Don’t reduce the runtime user to SELECT only.
Kong Gateway also writes to the database directly, even when no client calls the Admin API:
|
Write
|
Commands
|
When it happens
|
|
Cache invalidation events between nodes
|
INSERT, DELETE
|
On every configuration change, on each node.
|
|
Data plane heartbeat records
|
INSERT, UPDATE
|
When a data plane node connects to the control plane in Hybrid mode.
|
|
Expired row cleanup
|
DELETE
|
On a background timer, every 300 seconds by default.
|
|
Lock release
|
DELETE
|
When Kong Gateway completes a task that uses a database lock.
|
|
Rate limit counters
|
INSERT, UPDATE
|
On proxy traffic, when the rate-limiting plugin uses policy = cluster.
|
|
Plugin DAO writes
|
INSERT, UPDATE
|
On proxy traffic through a plugin that persists its own state, for example the oauth2 plugin, which stores tokens and authorization codes.
|
|
Audit logs (Enterprise)
|
INSERT
|
On Admin API activity, when audit_log = on.
|
The runtime session never runs TRUNCATE. Grant TRUNCATE only to the role that runs admin CLI commands.
Applies to:
- Traditional mode with
database = postgres.
- Hybrid mode control plane nodes (
role = control_plane).
Does not apply to the following commands:
kong config db_import and kong config db_export fail immediately when database = off (traditional DB-less mode and hybrid mode data planes).
kong workspace manages workspace rows that exist only in a database-backed deployment.
If the same role runs the admin CLI and migrations, skip this section. Ownership already covers it. If you configure a different write user for admin CLI commands, grant it the runtime privileges, plus the following privileges:
|
Privilege
|
On
|
Purpose
|
TRUNCATE
|
Clustering sync tables
|
kong workspace and kong config db_import need this privilege. Both commands call truncate_clustering_sync_version().
|
The read-only user is optional. If you don’t point pg_ro_host at a read replica, skip this section. Without it, Kong Gateway never activates the read-only connector. The read-only role doesn’t need to exist.
Only the migration-time write user owns the schema and its tables. As a result, only that user can grant privileges on them. Connect as the migration-time write user or a superuser. Run the following SQL commands to configure the read-only user:
-- [DCL] Allow connection to the database (default on)
GRANT CONNECT ON DATABASE $PG_DATABASE TO $PG_RO_USER;
-- [DCL] Allow access to the schema
GRANT USAGE ON SCHEMA $PG_SCHEMA TO $PG_RO_USER;
-- [DCL] Allow SELECT on existing tables created by the migration-time write user
GRANT SELECT ON ALL TABLES IN SCHEMA $PG_SCHEMA TO $PG_RO_USER;
-- [DCL] Auto-grant SELECT on future tables created by the migration-time write user
ALTER DEFAULT PRIVILEGES FOR ROLE $PG_USER IN SCHEMA $PG_SCHEMA GRANT SELECT ON TABLES TO $PG_RO_USER;
Replace the following variables with the actual values:
$PG_SCHEMA with the name of the schema (for example, public).
$PG_RO_USER with the value of pg_ro_user.
$PG_USER with name of your user (for example, kong).