w3resource

Grant SELECT Privileges to a User in PostgreSQL


Grant SELECT Privileges to a User

Write a PostgreSQL query to grant SELECT privileges on the employees table to the user data_analyst.

Solution:

-- Grant SELECT privileges to a user
GRANT SELECT ON employees TO data_analyst;

Explanation:

  • Purpose of the Query:
    • Gives read-only access to the employees table
  • Key Components:
    • GRANT SELECT ON employees: Grants permission to query the employees table.
    • TO data_analyst: Specifies the recipient of the privilege.
  • Real-World Application:
    • Used in scenarios where analysts need read-only access to data.

Notes:

  • Without SELECT privilege, the user cannot query the table.
  • Use REVOKE SELECT ON employees FROM data_analyst; to remove access.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to grant SELECT and UPDATE privileges on the customer_info table to the user client_manager.
  • Write a PostgreSQL query to grant only SELECT privileges on multiple tables (employees, departments, salaries) to the user hr_specialist.
  • Write a PostgreSQL query to grant SELECT privileges on all tables in a specific schema to the role reporting_team.
  • Write a PostgreSQL query to grant SELECT privileges on a table but restrict access to a specific column.


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous PostgreSQL Exercise: Create a User with a Password.

Next PostgreSQL Exercise: Revoke a User’s Privileges.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.