Migrating a PostgreSQL database isn’t just about moving data—getting the right roles and permissions in place is critical for security and proper application function. This post demonstrates how to extract roles and permissions from your source instance and apply them to a new PostgreSQL environment.
Understanding Permissions in PostgreSQL
PostgreSQL controls access via roles. A role can represent either a database user or a group, and roles are granted permissions (privileges) on objects (tables, databases, schemas, etc.) using GRANT and REVOKE commands. These permissions can be viewed and managed at various levels:
- Database: Control who can connect
- Schema: Control access to groups of tables, functions, etc.
- Objects: Control what actions (SELECT, INSERT, UPDATE, etc.) users can perform on tables, functions, or sequences.
You can view permissions using PostgreSQL’s psql meta-commands:
\l+— Show database privileges\dn+— Show schema privileges\dp+— Show table and other object privileges
Step 1: Extract Roles from the Source Instance
To extract roles (users and groups) from your current PostgreSQL server:
pg_dumpall --roles-only > roles.sql
Note:
- This command will export all roles (but not their passwords in managed services like Azure Database for PostgreSQL).
- In cloud managed systems, you might not have the ability to extract passwords; you’ll need to set them manually on the target instance.
Step 2: Extract Role & Object Permissions
Extracting object-level permissions (like all GRANT and REVOKE statements) can be done while dumping database schema:
pg_dump -h <source_server> -U <username> -d <dbname> -s > db_schema.sql
Next, filter permission statements:
If you’re working in PowerShell (Windows), run:
pg_dump -h "psql-prod-01.postgres.database.azure.com" -U pgadmin -d "prod_db" -s | Select-String -Pattern "^(GRANT|REVOKE|ALTER DEFAULT PRIVILEGES)" -Path "C:\Path\to\db_schema.sql" | ForEach-Object { $_.Line } > C:\Path\to\permissions.sql
If you’re working on Mac/Linux, run:
pg_dump -h "psql-prod-01.postgres.database.azure.com" -U pgadmin -d "prod_db" -s | grep -E '^(GRANT|REVOKE|ALTER DEFAULT PRIVILEGES)' > perms.sql
This extracts all lines related to granting or revoking privileges and puts them in perms.sql.
Step 3: Prepare and Edit Scripts
- Review the extracted
roles.sqland `permissions.sql:- Remove any references to unsupported roles (like
postgressuperuser in cloud environments). - Plan to set user passwords manually if they weren’t included.
- Remove any references to unsupported roles (like
Step 4: Copy Roles and Permissions to the New Instance
- Recreate roles:text
psql -h <target_server> -U <admin_user> -f roles.sql- Remember to set or update passwords for each user after creation.
- Apply object-level permissions:text
psql -h <target_server> -U <admin_user> -d <target_db> -f perms.sql
Step 5: Validate Permissions
Connect as each role or user to ensure operations work as expected: