WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Intermediate · question 36 of 100

How do you manage user roles and permissions in PostgreSQL?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

In PostgreSQL, user roles and permissions are managed using the role-based access control (RBAC) system. The RBAC system allows you to define roles and assign permissions to those roles, and then assign those roles to users.

Here’s a brief overview of the steps involved:

1. Create roles: Use the ‘CREATE ROLE‘ command to create roles in PostgreSQL. A role can be either a database user or a group of users. For example, to create a role for a database user, you can use the following command:

   CREATE ROLE myuser LOGIN PASSWORD 'mypassword';

This creates a new role named ‘myuser‘ with a login capability and a password.

2. Grant permissions to roles: Use the ‘GRANT‘ command to grant permissions to roles in PostgreSQL. For example, to grant ‘SELECT‘ permission on a table to a role, you can use the following command:

   GRANT SELECT ON mytable TO myrole;

This grants the ‘SELECT‘ permission on the ‘mytable‘ table to the ‘myrole‘ role.

3. Assign roles to users: Use the ‘ALTER USER‘ or ‘ALTER ROLE‘ command to assign roles to users in PostgreSQL. For example, to assign the ‘myrole‘ role to the ‘myuser‘ user, you can use the following command:

   ALTER USER myuser WITH ROLE myrole;

This assigns the ‘myrole‘ role to the ‘myuser‘ user.

You can also use the ‘REVOKE‘ command to revoke permissions from roles and the ‘DROP ROLE‘ command to delete roles.

Here’s an example Java code snippet that demonstrates how to create a role in PostgreSQL:

import java.sql.*;

public class PostgresExample {
  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    String role = "myuser";
    String password = "mypassword";
    String createRoleSQL = "CREATE ROLE " + role + " LOGIN PASSWORD '" + password + "';";
    
    try {
      conn = DriverManager.getConnection("jdbc:postgresql://localhost/mydatabase", "myusername", "mypassword");
      stmt = conn.createStatement();
      stmt.execute(createRoleSQL);
      System.out.println("Role created successfully");
    } catch (SQLException e) {
      System.out.println("Error creating role: " + e.getMessage());
    } finally {
      try {
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
      } catch (SQLException e) {
        System.out.println("Error closing connection: " + e.getMessage());
      }
    }
  }
}

This code creates a new role named ‘myuser‘ with a login capability and a password in the ‘mydatabase‘ database. Note that you need to replace ‘myusername‘ and ‘mypassword‘ with your PostgreSQL username and password.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics