w3resource

How to Create a PostgreSQL User with a Password


Create a User with a Password

Write a PostgreSQL query to create a new user named app_user with a password.

Solution:

-- Create a user with a password
CREATE USER app_user WITH PASSWORD 'securePass123';

Explanation:

  • Purpose of the Query:
    • Creates a new user (app_user) and assigns a password.
  • Key Components:
    • CREATE USER app_user: Defines a new user.
    • WITH PASSWORD 'securePass123': Sets a password for authentication.
  • Real-World Application:
    • Necessary for applications that require database authentication.

Notes:

  • The password should be strong and stored securely.
  • PostgreSQL can enforce password policies (e.g., expiration, complexity).

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to create a user named dev_user with a password that requires a reset on the next login.
  • Write a PostgreSQL query to create a user named audit_user with a strong password policy enforced.
  • Write a PostgreSQL query to create a user named hr_manager with a password that never expires.
  • Write a PostgreSQL query to create a user named api_service with a password that is stored in encrypted format.


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

Previous PostgreSQL Exercise: Create a New Role.

Next PostgreSQL Exercise: Grant SELECT Privileges to a User.

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.