May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
-- Rename a user:
|
||||
|
||||
postgres=# alter user dbatest rename to dbaprod;
|
||||
NOTICE: MD5 password cleared because of role rename
|
||||
ALTER ROLE
|
||||
postgres=# \du
|
||||
List of roles
|
||||
Role name | Attributes | Member of
|
||||
-----------+------------------------------------------------------------+-----------
|
||||
dbaprod | | {}
|
||||
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
|
||||
|
||||
<< NOTE - AFTER renaming the user, you need to reset the password to same old one.
|
||||
|
||||
i.e
|
||||
-- Change the password a user:
|
||||
|
||||
postgres=# alter user dbaprod password 'test';
|
||||
ALTER ROLE
|
||||
|
||||
--- Increase the validity of the user:
|
||||
|
||||
postgres=# alter user dbaprod valid until 'Feb 10 2021';
|
||||
ALTER ROLE
|
||||
@@ -0,0 +1,23 @@
|
||||
-- Find search_path of users in a particular database ( replace your db_name(EDB))
|
||||
|
||||
SELECT r.rolname, d.datname, drs.setconfig
|
||||
FROM pg_db_role_setting drs
|
||||
LEFT JOIN pg_roles r ON r.oid = drs.setrole
|
||||
LEFT JOIN pg_database d ON d.oid = drs.setdatabase
|
||||
WHERE d.datname = 'EDB';
|
||||
|
||||
-- Find search_path of users in postgres db cluster( all database)
|
||||
|
||||
SELECT r.rolname, d.datname, drs.setconfig
|
||||
FROM pg_db_role_setting drs
|
||||
LEFT JOIN pg_roles r ON r.oid = drs.setrole
|
||||
LEFT JOIN pg_database d ON d.oid = drs.setdatabase;
|
||||
|
||||
|
||||
-- set search_path for a user in particular db:
|
||||
|
||||
postgres# alter user prod_user in database "EDB" set search_path="$user", public, prim_db;
|
||||
|
||||
-- set search_path for a user in postgres cluster( all dbs)
|
||||
|
||||
postgres=# alter user prod_user set search_path="$user", public, prim_db;
|
||||
@@ -0,0 +1,21 @@
|
||||
List roles :
|
||||
|
||||
postgres=# select rolname,rolcanlogin,rolvaliduntil from pg_roles;
|
||||
rolname | rolcanlogin | rolvaliduntil
|
||||
----------------------+-------------+---------------------------
|
||||
pg_monitor | f |
|
||||
pg_read_all_settings | f |
|
||||
pg_read_all_stats | f |
|
||||
pg_stat_scan_tables | f |
|
||||
pg_signal_backend | f |
|
||||
postgres | t |
|
||||
test_dbuser1 | f | 2020-08-08 00:00:00+05:30
|
||||
|
||||
|
||||
|
||||
rolcanlogin - > If true mean they are role as well as user
|
||||
If false mean they are only role( they cannot login)
|
||||
|
||||
NOTE - > In postgres users are bydefault role, but roles are not bydefault user. i.e
|
||||
|
||||
Bydefault user come with login privilege, where as roles don’t come with login privilege.
|
||||
@@ -0,0 +1,29 @@
|
||||
--- List users present in postgres:
|
||||
|
||||
postgres=# select usename,usesuper,valuntil from pg_user;
|
||||
usename | usesuper | valuntil
|
||||
---------------+----------+---------------------------
|
||||
postgres | t |
|
||||
test_dbuser1 | f | 2020-08-08 00:00:00+05:30
|
||||
|
||||
postgres#select usename,usesuper,valuntil from pg_shadow;
|
||||
|
||||
usename | usesuper | valuntil
|
||||
---------------+----------+---------------------------
|
||||
postgres | t |
|
||||
test_dbuser1 | f | 2020-08-08 00:00:00+05:30
|
||||
|
||||
postgres#select usename,usesuper,valuntil from pg_shadow;
|
||||
|
||||
postgres=# \du
|
||||
List of roles
|
||||
Role name | Attributes | Member of
|
||||
---------------+------------------------------------------------------------+-----------
|
||||
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
|
||||
test_dbuser1 | Password valid until 2020-08-08 00:00:00+05:30 | {}
|
||||
|
||||
|
||||
|
||||
NOTE - > \du command output includes both user and roles(custom created roles only).
|
||||
|
||||
postgres users are bydefault role, but roles are not bydefault user.
|
||||
@@ -0,0 +1,14 @@
|
||||
--- in edb postgres advanced server we can create user profile
|
||||
---- similar to that of oracle.
|
||||
|
||||
-- Create profile:
|
||||
|
||||
# create profile REPORTING_PROFILE limit FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LIFE_TIME 90;
|
||||
|
||||
--- Alter profile:
|
||||
|
||||
# alter profile REPORTING_PROFILE limit FAILED_LOGIN_ATTEMPTS 1;
|
||||
|
||||
-- view profile details:
|
||||
|
||||
# select * from dba_profiles;
|
||||
@@ -0,0 +1,19 @@
|
||||
- Create role :
|
||||
|
||||
dbaclass=# create role dev_admin;
|
||||
CREATE ROLE
|
||||
|
||||
dbaclass=# create role dev_admin with valid until '10-oct-2020';
|
||||
CREATE ROLE
|
||||
|
||||
-- role with createdb and superuser privilege and login keyword mean it can login to db like a normal user
|
||||
|
||||
dbaclass=# create role dev_admin with createdb createrole login ;
|
||||
CREATE ROLE
|
||||
|
||||
DROP ROLE:
|
||||
|
||||
dbaclass=# drop role dev_admin;
|
||||
DROP ROLE
|
||||
|
||||
select rolname,rolcanlogin,rolvaliduntil from pg_roles;
|
||||
@@ -0,0 +1,27 @@
|
||||
• A schema is a named collection of tables. A schema can also contain views, indexes, sequences, data types, operators, and functions.
|
||||
|
||||
--- Create schema:
|
||||
|
||||
postgres=# create schema dba_schema;
|
||||
CREATE SCHEMA
|
||||
|
||||
-- Create schema with authorize particular user:
|
||||
|
||||
postgres=# create schema dba_schema authorization raj2;
|
||||
CREATE SCHEMA
|
||||
|
||||
-- Drop schema
|
||||
|
||||
postgres=# drop schema dba_schema;
|
||||
DROP SCHEMA
|
||||
|
||||
-- List down schemas present
|
||||
|
||||
postgres=# \dn+
|
||||
List of schemas
|
||||
Name | Owner. | Access privileges | Description
|
||||
------------+----------+----------------------+------------------------
|
||||
dba_schema | raj2 | |
|
||||
public | postgres | postgres=UC/postgres+| standard public schema| | =UC/postgres |
|
||||
raj. | postgres | |
|
||||
(3 rows)
|
||||
@@ -0,0 +1,83 @@
|
||||
CREATE USER:
|
||||
|
||||
dbaclass=# create user TEST_DBACLASS with password 'test123';
|
||||
CREATE ROLE
|
||||
|
||||
CREATE USER WITH VALID UNTIL:
|
||||
|
||||
dbaclass=# create user TEST_dbuser1 with password 'test123' valid until '2020-08-08';
|
||||
CREATE ROLE
|
||||
|
||||
CREATE USER WITH SUPER USER PRIVILEGE
|
||||
|
||||
dbaclass=# create user test_dbuser3 with password 'test123' CREATEDB SUPERUSER;
|
||||
|
||||
CREATE ROLE
|
||||
|
||||
VIEW USERS:
|
||||
|
||||
dbaclass=# select usename,valuntil,usecreatedb from pg_shadow;
|
||||
|
||||
dbaclass=# select usename,usesuper,valuntil from pg_user;
|
||||
|
||||
dbaclass=# \du+
|
||||
|
||||
DROP USER:
|
||||
|
||||
drop user DB_user1;
|
||||
|
||||
|
||||
-- providing superuser role will make an user superuser.
|
||||
|
||||
postgres=# select usename,usesuper from pg_user where usename='dbatest';
|
||||
usename | usesuper
|
||||
---------+----------
|
||||
dbatest | f
|
||||
(1 row)
|
||||
|
||||
postgres=#
|
||||
postgres=# alter user dbatest with superuser;
|
||||
ALTER ROLE
|
||||
postgres=# select usename,usesuper from pg_user where usename='dbatest';
|
||||
usename | usesuper
|
||||
---------+----------
|
||||
dbatest | t
|
||||
|
||||
-- How to revoke superuser:
|
||||
|
||||
postgres=# alter user dbatest with nosuperuser;
|
||||
ALTER ROLE
|
||||
postgres=#
|
||||
postgres=# select usename,usesuper from pg_user where usename='dbatest';
|
||||
usename | usesuper
|
||||
---------+----------
|
||||
dbatest | f
|
||||
(1 row)
|
||||
|
||||
|
||||
-- providing superuser role will make an user superuser.
|
||||
|
||||
postgres=# select usename,usesuper from pg_user where usename='dbatest';
|
||||
usename | usesuper
|
||||
---------+----------
|
||||
dbatest | f
|
||||
(1 row)
|
||||
|
||||
postgres=#
|
||||
postgres=# alter user dbatest with superuser;
|
||||
ALTER ROLE
|
||||
postgres=# select usename,usesuper from pg_user where usename='dbatest';
|
||||
usename | usesuper
|
||||
---------+----------
|
||||
dbatest | t
|
||||
|
||||
-- How to revoke superuser:
|
||||
|
||||
postgres=# alter user dbatest with nosuperuser;
|
||||
ALTER ROLE
|
||||
postgres=#
|
||||
postgres=# select usename,usesuper from pg_user where usename='dbatest';
|
||||
usename | usesuper
|
||||
---------+----------
|
||||
dbatest | f
|
||||
(1 row)
|
||||
Reference in New Issue
Block a user