|
| 1 | +--- |
| 2 | +title: PostgreSQL Permissions - Guide |
| 3 | +description: >- |
| 4 | + How to create and configure PostgreSQL users with the necessary permissions for Electric. |
| 5 | +outline: [2, 3] |
| 6 | +--- |
| 7 | + |
| 8 | +# PostgreSQL Permissions |
| 9 | + |
| 10 | +This guide explains how to create PostgreSQL users with the necessary permissions for Electric to work correctly. Electric requires specific database privileges to enable [logical replication](https://www.postgresql.org/docs/current/logical-replication.html) and manage publications. |
| 11 | + |
| 12 | +## Quick Start |
| 13 | + |
| 14 | +Choose the approach that fits your needs: |
| 15 | + |
| 16 | +**For Development:** |
| 17 | +Use the default `postgres` superuser. Electric will automatically configure everything. |
| 18 | + |
| 19 | +```shell |
| 20 | +DATABASE_URL=postgresql://postgres:your_password@localhost:5432/your_database |
| 21 | +``` |
| 22 | + |
| 23 | +This is often fine for production too. See [For Development](#for-development) below for details. |
| 24 | + |
| 25 | +**For Production (Automatic Mode):** |
| 26 | +Create a dedicated Electric user with automatic table configuration. |
| 27 | +→ See [Automatic Mode Setup](#automatic-mode-setup) |
| 28 | + |
| 29 | +**For Production (Manual Mode / Least-Privilege):** |
| 30 | +Pre-configure tables manually and use minimal Electric privileges. |
| 31 | +→ See [Manual Mode Setup](#manual-mode-setup) |
| 32 | + |
| 33 | +## Core Permission Requirements |
| 34 | + |
| 35 | +Electric needs the following PostgreSQL permissions: |
| 36 | + |
| 37 | +| Permission | Purpose | Required For | |
| 38 | +|------------|---------|--------------| |
| 39 | +| [`REPLICATION`](https://www.postgresql.org/docs/current/sql-createrole.html) | Enable logical replication streaming | Creating [replication slots](https://www.postgresql.org/docs/current/warm-standby.html#STREAMING-REPLICATION-SLOTS) and consuming the [WAL](https://www.postgresql.org/docs/current/wal-intro.html) | |
| 40 | +| [`CREATE`](https://www.postgresql.org/docs/current/ddl-priv.html) on database | Create publications | Automatic publication management | |
| 41 | +| [`SELECT`](https://www.postgresql.org/docs/current/sql-grant.html) on tables | Read table data | Initial shape snapshots | |
| 42 | +| [Table ownership](https://www.postgresql.org/docs/current/ddl-priv.html) | Set replica identity | Configuring [`REPLICA IDENTITY FULL`](https://www.postgresql.org/docs/current/sql-altertable.html) | |
| 43 | +| [Publication ownership](https://www.postgresql.org/docs/current/sql-createpublication.html) | Modify publications | Adding/removing tables from publication; you must also own each table you add to the publication | |
| 44 | + |
| 45 | +## Automatic vs Manual Publication Management |
| 46 | + |
| 47 | +Electric can operate in two modes: |
| 48 | + |
| 49 | +### 1. Automatic Mode (Recommended) |
| 50 | + |
| 51 | +Electric automatically creates the publication and adds tables to it as shapes are requested. Requires `CREATE` privilege on the database and either table ownership or pre-configured `REPLICA IDENTITY FULL` on tables. |
| 52 | + |
| 53 | +### 2. Manual Mode |
| 54 | + |
| 55 | +You manually manage the publication and Electric only validates the configuration. Enable this with [`ELECTRIC_MANUAL_TABLE_PUBLISHING=true`](/docs/api/config#electric-manual-table-publishing). Requires only `REPLICATION` and `SELECT` privileges, but you must pre-configure the publication and `REPLICA IDENTITY FULL`. |
| 56 | + |
| 57 | +**When to use manual mode:** |
| 58 | +- When Electric's database user doesn't have `CREATE` privileges |
| 59 | +- When you want explicit control over which tables are replicated |
| 60 | +- In security-sensitive environments with strict privilege separation |
| 61 | + |
| 62 | +## Setup Examples |
| 63 | + |
| 64 | +### For Development |
| 65 | + |
| 66 | +Use the default `postgres` superuser or another superuser role: |
| 67 | + |
| 68 | +```shell |
| 69 | +DATABASE_URL=postgresql://postgres:your_password@localhost:5432/your_database |
| 70 | +``` |
| 71 | + |
| 72 | +Electric will automatically create publications, configure `REPLICA IDENTITY FULL`, and manage everything for you. This is often fine for production too, especially for smaller deployments. |
| 73 | + |
| 74 | +### Automatic Mode Setup |
| 75 | + |
| 76 | +Create a dedicated Electric user with automatic table configuration. Electric will create publications and add tables as shapes are requested. |
| 77 | + |
| 78 | +```sql |
| 79 | +-- Create the Electric user with REPLICATION |
| 80 | +CREATE ROLE electric_user WITH LOGIN PASSWORD 'secure_password' REPLICATION; |
| 81 | + |
| 82 | +-- Grant database-level privileges |
| 83 | +GRANT CONNECT ON DATABASE mydb TO electric_user; |
| 84 | +GRANT USAGE, CREATE ON SCHEMA public TO electric_user; |
| 85 | +GRANT CREATE ON DATABASE mydb TO electric_user; |
| 86 | + |
| 87 | +-- Grant SELECT on tables (Electric is read-only, only needs to read data) |
| 88 | +-- For all tables: |
| 89 | +GRANT SELECT ON ALL TABLES IN SCHEMA public TO electric_user; |
| 90 | + |
| 91 | +-- Grant SELECT on future tables automatically (https://www.postgresql.org/docs/current/sql-alterdefaultprivileges.html) |
| 92 | +ALTER DEFAULT PRIVILEGES IN SCHEMA public |
| 93 | + GRANT SELECT ON TABLES TO electric_user; |
| 94 | + |
| 95 | +-- For specific tables only: |
| 96 | +-- GRANT SELECT ON public.users, public.posts TO electric_user; |
| 97 | +``` |
| 98 | + |
| 99 | +**Handling REPLICA IDENTITY FULL - Choose one option:** |
| 100 | + |
| 101 | +**Option A: Pre-configure as DBA (Recommended)** |
| 102 | + |
| 103 | +Have your DBA or a superuser set `REPLICA IDENTITY FULL` before Electric runs, keeping table ownership with your application: |
| 104 | + |
| 105 | +```sql |
| 106 | +-- As superuser/DBA, set REPLICA IDENTITY on tables that will be synced |
| 107 | +ALTER TABLE public.users REPLICA IDENTITY FULL; |
| 108 | +ALTER TABLE public.posts REPLICA IDENTITY FULL; |
| 109 | +ALTER TABLE public.comments REPLICA IDENTITY FULL; |
| 110 | +``` |
| 111 | + |
| 112 | +**Option B: Transfer Ownership to Electric** |
| 113 | + |
| 114 | +Transfer table ownership so Electric can manage `REPLICA IDENTITY` automatically: |
| 115 | + |
| 116 | +```sql |
| 117 | +DO $$ |
| 118 | +DECLARE |
| 119 | + r RECORD; |
| 120 | +BEGIN |
| 121 | + FOR r IN SELECT tablename FROM pg_tables WHERE schemaname = 'public' |
| 122 | + LOOP |
| 123 | + EXECUTE 'ALTER TABLE public.' || quote_ident(r.tablename) || ' OWNER TO electric_user'; |
| 124 | + END LOOP; |
| 125 | +END$$; |
| 126 | +``` |
| 127 | + |
| 128 | +> [!Warning] Ownership Transfer |
| 129 | +> Transferring table ownership removes ownership from the previous owner. If you prefer to keep table ownership with your application or DBA role, use Option A instead. |
| 130 | +
|
| 131 | +Then connect Electric: |
| 132 | + |
| 133 | +```shell |
| 134 | +DATABASE_URL=postgresql://electric_user:secure_password@localhost:5432/mydb |
| 135 | +``` |
| 136 | + |
| 137 | +### Manual Mode Setup |
| 138 | + |
| 139 | +For environments with strict security requirements, you can use manual publication management to minimize Electric's privileges. |
| 140 | + |
| 141 | +```sql |
| 142 | +-- Create the Electric user with REPLICATION (but minimal other privileges) |
| 143 | +CREATE ROLE electric_user WITH LOGIN PASSWORD 'secure_password' REPLICATION; |
| 144 | + |
| 145 | +-- Grant only connection and usage privileges |
| 146 | +GRANT CONNECT ON DATABASE mydb TO electric_user; |
| 147 | +GRANT USAGE ON SCHEMA public TO electric_user; |
| 148 | + |
| 149 | +-- Grant SELECT only on specific tables |
| 150 | +GRANT SELECT ON public.users TO electric_user; |
| 151 | +GRANT SELECT ON public.posts TO electric_user; |
| 152 | +``` |
| 153 | + |
| 154 | +Then, as a superuser or database owner, follow the [Manual Configuration Steps](#manual-configuration-steps) below to create the publication, add tables, and configure replica identity. |
| 155 | + |
| 156 | +Configure Electric with: |
| 157 | + |
| 158 | +```shell |
| 159 | +DATABASE_URL=postgresql://electric_user:secure_password@localhost:5432/mydb |
| 160 | +ELECTRIC_MANUAL_TABLE_PUBLISHING=true |
| 161 | +``` |
| 162 | + |
| 163 | +## AWS RDS and Aurora |
| 164 | + |
| 165 | +AWS RDS and Aurora require special handling for replication permissions. See the [AWS integration guide](/docs/integrations/aws) for details on enabling logical replication and granting the `rds_replication` role. |
| 166 | + |
| 167 | +## Manual Configuration Steps |
| 168 | + |
| 169 | +If you need to manually configure the publication and replica identity (for use with `ELECTRIC_MANUAL_TABLE_PUBLISHING=true`): |
| 170 | + |
| 171 | +### 1. [Create the Publication](https://www.postgresql.org/docs/current/sql-createpublication.html) |
| 172 | + |
| 173 | +```sql |
| 174 | +-- Create an empty publication |
| 175 | +CREATE PUBLICATION electric_publication_default; |
| 176 | + |
| 177 | +-- Or with a custom name (configure with ELECTRIC_REPLICATION_STREAM_ID) |
| 178 | +CREATE PUBLICATION my_custom_publication; |
| 179 | +``` |
| 180 | + |
| 181 | +### 2. [Add Tables to the Publication](https://www.postgresql.org/docs/current/sql-alterpublication.html) |
| 182 | + |
| 183 | +```sql |
| 184 | +-- Add specific tables |
| 185 | +ALTER PUBLICATION electric_publication_default ADD TABLE public.users; |
| 186 | +ALTER PUBLICATION electric_publication_default ADD TABLE public.posts; |
| 187 | +ALTER PUBLICATION electric_publication_default ADD TABLE public.comments; |
| 188 | +``` |
| 189 | + |
| 190 | +### 3. [Configure Replica Identity](https://www.postgresql.org/docs/current/sql-altertable.html) |
| 191 | + |
| 192 | +For each table you want to sync, you must set the replica identity to `FULL`: |
| 193 | + |
| 194 | +```sql |
| 195 | +ALTER TABLE public.users REPLICA IDENTITY FULL; |
| 196 | +ALTER TABLE public.posts REPLICA IDENTITY FULL; |
| 197 | +ALTER TABLE public.comments REPLICA IDENTITY FULL; |
| 198 | +``` |
| 199 | + |
| 200 | +This tells Postgres to include all column values in the replication stream, which Electric requires for accurate change tracking. |
| 201 | + |
| 202 | +### 4. Set Publication Ownership |
| 203 | + |
| 204 | +Make the Electric user the owner of the publication (or ensure the publication was created by the same user Electric connects as): |
| 205 | + |
| 206 | +```sql |
| 207 | +ALTER PUBLICATION electric_publication_default OWNER TO electric_user; |
| 208 | +``` |
| 209 | + |
| 210 | +### 5. Verify the Configuration |
| 211 | + |
| 212 | +Check that your publication is correctly configured: |
| 213 | + |
| 214 | +```sql |
| 215 | +-- List all publications |
| 216 | +SELECT * FROM pg_publication; |
| 217 | + |
| 218 | +-- Check publication settings |
| 219 | +SELECT pubname, pubinsert, pubupdate, pubdelete, pubtruncate |
| 220 | +FROM pg_publication |
| 221 | +WHERE pubname = 'electric_publication_default'; |
| 222 | + |
| 223 | +-- List tables in the publication |
| 224 | +SELECT schemaname, tablename |
| 225 | +FROM pg_publication_tables |
| 226 | +WHERE pubname = 'electric_publication_default'; |
| 227 | + |
| 228 | +-- Check replica identity for tables |
| 229 | +SELECT schemaname, tablename, relreplident |
| 230 | +FROM pg_class |
| 231 | +JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid |
| 232 | +WHERE relreplident = 'f'; -- 'f' means FULL |
| 233 | +``` |
| 234 | + |
| 235 | +## Troubleshooting |
| 236 | + |
| 237 | +For common permission errors and their solutions, see the [Database permissions section](/docs/guides/troubleshooting#database-permissions-how-do-i-configure-postgresql-users-for-electric) in the main Troubleshooting guide. |
| 238 | + |
| 239 | +## Next Steps |
| 240 | + |
| 241 | +- Review the [Deployment guide](/docs/guides/deployment) for production setup |
| 242 | +- Learn about [Security](/docs/guides/security) best practices |
| 243 | +- See [Troubleshooting](/docs/guides/troubleshooting) for common issues |
| 244 | +- Check the [Configuration reference](/docs/api/config) for all available options |
0 commit comments