- ERROR: must be member of role "test"
Last active
January 13, 2025 11:38
-
-
Save tuannvm/25a333011bfb9080293499df627ce3da to your computer and use it in GitHub Desktop.
#postgres #cheatsheet
- Create db and grant privilege to user:
CREATE DATABASE <db-name>;
CREATE USER <user-name> WITH PASSWORD <password>;
GRANT ALL PRIVILEGES ON DATABASE <db-name> to <user-name>;
Access psql:
psql -U <username> -d <dbname> -h <host>
you must use the same password for postgres master/slave DBs
- Backup & Restore. Reference:
# backup
pg_dump -U $username --format=c --file=$mydatabase.sqlc $dbname
# dump without owner & privileges
pg_dump -U $username --format=c --file=$mydatabase.sqlc -O -x $dbname
# -O, --no-owner skip restoration of object ownership in plain-text format
# -x, --no-privileges do not dump privileges (grant/revoke)
---
# restore only a specified index from a backup file:
pg_restore -U $username --dbname=$dbname --index=$indexname
# restore only a single function:
pg_restore -U $username --dbname=$dbname --function=$functionname(args)
# restore only a single table:
pg_restore -U $username --dbname=$dbname --table=$tablename
# simple restore:
pg_restore -U$username --dbname=$databasename $filename
- Query all postgres settings:
SELECT name,setting,unit FROM pg_settings;
- Create new schema and grant privileges:
CREATE USER abc WITH PASSWORD 'ks839#@S';
CREATE SCHEMA abc;
GRANT ALL ON ALL TABLES IN SCHEMA abc TO abc;
ALTER DEFAULT PRIVILEGES IN SCHEMA abc GRANT ALL ON TABLES TO abc;
GRANT ALL PRIVILEGES ON SCHEMA abc TO abc;
- Read-only user:
GRANT SELECT ON ALL TABLES IN SCHEMA public TO abc;
GRANT USAGE ON SCHEMA public TO abc;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO abc;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.