DEV Community

Cover image for How to create new user, and database in Postgres and grant and revoke permission
Md Enayetur Rahman
Md Enayetur Rahman

Posted on

How to create new user, and database in Postgres and grant and revoke permission

  • Open SQL Shell.

  • Provide your user name and password.

  • Type following command

CREATE DATABASE your_database_name;
Enter fullscreen mode Exit fullscreen mode
  • If database create successfully you will see "CREATE DATABASE" message.

  • Type following command to see all the database.

\l
Enter fullscreen mode Exit fullscreen mode
  • If you want to see your user name and database in which you are connected you can run following command.
\conninfo
Enter fullscreen mode Exit fullscreen mode
  • You will see a message like following

You are connected to database "postgres" as user "postgres" on host "localhost" (address "::1") at port "5432".
postgres=#

  • It will show your database name, user name, host and port. In my case db name is postgres, user name is postgres, host is localhost and port is 5432.

  • If we want to see the schema then you can type following command

\dn
Enter fullscreen mode Exit fullscreen mode
  • For me it is showing following
      List of schemas
  Name  |       Owner
--------+-------------------
 public | pg_database_owner
(1 row)
Enter fullscreen mode Exit fullscreen mode
  • If you want to see the users you can run following command
\du
Enter fullscreen mode Exit fullscreen mode
  • For me it is showing following
                             List of roles
 Role name |                         Attributes

-----------+------------------------------------------------------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS
Enter fullscreen mode Exit fullscreen mode
  • Currently there is one user named "postgres" and he has "Superuser, Create role, Create DB, Replication, Bypass RLS" attributes. Attributes are collection of privileges. In simple term we can say the permission of the user to do certain task in the database.

  • If you want to create an user with password you can use following command

CREATE USER abd WITH LOGIN ENCRYPTED PASSWORD '123456';
Enter fullscreen mode Exit fullscreen mode
  • It will create an user with the name of abd and the password will be 123456.

  • If user is created successfully you will see CREATE ROLE message in the terminal.

  • If you want to create a role you can use following command

CREATE ROLE efg WITH LOGIN ENCRYPTED PASSWORD '654321';
Enter fullscreen mode Exit fullscreen mode
  • It will create a role with the name of efg and the password will be 654321.

  • If user is created successfully you will see CREATE ROLE message in the terminal.

If you type \du then you will see the user and role name.

  • By default the newly created user does not have any permission. Super admin have to grant permission and can also revoke permission.

  • Below is the command for granting all the privileges to an user on a specific table.

GRANT ALL PRIVILEGES ON TABLE table_name TO user_name;
Enter fullscreen mode Exit fullscreen mode
  • If you see "GRANT" message then the permission successfully granted. And the user now can perform read, write, update and delete operation in that table.

  • Instead of granting all the privileges you can grant specific permission.

GRANT SELECT ON TABLE table_name TO user_name;
Enter fullscreen mode Exit fullscreen mode
  • If you see "GRANT" message then the permission successfully granted. And the user now can perform read operation in that table. Like this you can grant individual permission like update, delete etc.

  • If you want to revoke permission you can use following command

REVOKE SELECT ON TABLE table_name FROM user_name;
Enter fullscreen mode Exit fullscreen mode
  • If you see "REVOKE" message then the permission successfully revoked. And the user now can not perform read operation in that table. Like this you can revoke individual permission like update, delete etc. Similarly you can revoke all the permission.

  • We can grant permission for a database to a specific user.

GRANT ALL PRIVILEGES ON DATABASE database_name TO user_name;  
Enter fullscreen mode Exit fullscreen mode

Top comments (0)