How to Change a User to Superuser in PostgreSQL?
Last Updated :
16 Feb, 2024
Changing a user to a superuser in PostgreSQL is a straightforward process that involves using commands to alter the user's role. PostgreSQL, as a robust open-source relational database management system, allows administrators to manage user roles efficiently. PostgreSQL provides the flexibility to adjust user roles dynamically, ensuring that the right users have the necessary permissions to perform specific tasks.
In this article, we are going to learn how we can change a USER to a Superuser. Postgresql is one of the famous open-source RDBMS used by many companies, and startups and is growing day by day. So, learning Postgresql will add a new skill to your inventory.
How to Change a User to Superuser in PostgreSQL?
The process of changing a user to a superuser involves two primary steps the first one is to create an ordinary user and then alter the user's role to superuser status. To create a user the CREATE USER
command is used to specify the username and any additional parameters as needed. Once the user is created the ALTER USER
or ALTER ROLE
command is used to modify the user's role to superuser. The following methods are used to Change a User to Superuser in PostgreSQL are as follow:
- Change an Ordinary User to a Superuser Using ALTER USER Statement
- Change an Ordinary User to a Superuser Using ALTER ROLE Statement
Let's set up an Environment for performing operations
Before changing the role of a user, we have to create an ordinary user with no role. We will use the query CREATE USER to create a new user with no superuser role. The user created with this query will be ordinary with a specific role.
Command:
CREATE USER <user_name>;
Output:
User CreatedExplanation:
- CREATE USER <user_name>: This command will create an ordinary user with name. Here we have created and user with name "ujjwal".
- CREATE ROLE: This shows that command is executed successfully and the user is created successfully without any error.
Let's List the Users with their Roles
Now as we have created an ordinary user with no specific role, but we might want to see the users and their respective roles. So, to perform that operation we have to use the respective command.
Command:
\du
Output:
user with their rolesExplanation:
- /du: This command is used to list all the users and with their specific roles.
- Here "d" means describe and "u" means users.
- Use this command whenever you want list or describe users with their specific roles.
- Here "ujjwal" user is just an ordinary user and doesn't have any attributes with it.
1. Change an Ordinary User to a Superuser Using ALTER USER Statement
Now we have already created an ordinary user, its time to change that USER to Superuser using ALTER USER statement. The statement syntax looks like this:
Syntax:
ALTER USER user-name WITH SUPERUSER;
Explanation:
- ALTER: is used to alter or a make changes to something , it might a table, user etc.
- USER: it is used to define that we want to alter or make changes in USER.
- WITH: it is used to interchange the attribute.
- SUPERUSER: Attribute we are assigning to user.
Example: Changing Ordinary User to Superuser Using ALTER USER Statement
Now we are going to alter role of ordinary user to superuser in Postgres using ALTER USER statement.
Command:
ALTER USER ujjwal WITH SUPERUSER;
Output:
Role altered using ALTER command
Now check the final result, with the below query:
Command:
\du
Output:
Role changedExplanation: As we can see we have successfully change an ordinary user to superuser using ALTER statement.
2. Change an Ordinary User to a Superuser Using ALTER ROLE Statement
We have so far covered all the related topics, like creating an ordinary user and then changing an ordinary USER to Superuser using ALTER USER statement.
Now, we are going to change ordinary user to superuser using ALTER ROLE Statement, the statement is similar to ALTER USER statement and does the same work. Lets look at the syntax:
Syntax:
ALTER ROLE user_name WITH SUPERUSER;
Explanation:
- ALTER: To make changer or alter attributes in user, table etc.
- ROLE: To define that we want to alter role of the user.
- WITH: To specify the role to change with.
- SUPERUSER: Attribute we are assigning to an ordinary user.
Example: Changing ordinary user to superuser using ALTER ROLE statement in Postgres
Now we are going to change ordinary User to Superuser, but here we are going to use ALTER ROLE Statement in postgres. For that purpose, we have already created a new ordinary user, follow the above explained steps to create a user in postgres. Let's Creating a new ordinary user to alter its role.
Command:
CREATE USER <username>;
Output:
user-createdNow, we will alter role using ALTER ROLE Statement are defined below.
Command:
ALTER ROLE zoro WITH SUPERUSER;
Output:
Role alteredExplanation:
- We have created a new ordinary user with name "zoro".
- We used ALTER ROLE command to change ordinary user that is "zoro" to Superuser.
- To see the changes we use "\du" command.
Conclusion
Overall, we have learned about How to Change a User to Superuser in PostgreSQL Using ALTER ROLE and ALTER USER will alter the role of an ordinary user to Superuser, it depends on your choice to use which command among both of these commands. Both statements are used with "WITH" to specify the attribute to change or alter with. Learning how to manage user roles in PostgreSQL is a valuable skill that can enhance your career prospects. Whether you're a developer, database administrator or IT professional, understanding PostgreSQL's user management features can help you effectively manage database access and permissions.
Similar Reads
How to use PostgreSQL Database in Django?
This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are
2 min read
How to Create a New User With Full Privileges in PostgreSQL?
PostgreSQL provides a way to give a user full privileges to do anything with the database. The database objects like schema, table, function, and so on. The 'GRANT' command is used in PostgreSQL to provide a user with any specific privileges or to override the role of the user. In this article, we a
6 min read
How to List All Users in PostgreSQL
If we need to access all the users in our PostgreSQL database, we are in the right place. Whether we are a database administrator or a developer, having a clear method to list all users is essential for managing access and permissions. This article will guide us through a simple and effective way to
4 min read
How to Reset Auto Increment Counter in PostgreSQL?
PostgreSQL is a powerful, open-source, object-relational database management system (DBMS) developed by a vibrant community. One common requirement for database administrators is resetting the auto-increment counter for primary key sequences. In PostgreSQL, this process is crucial for ensuring the o
4 min read
How to Change The Primary Group of a User in Linux?
In the field of Linux system administration, learning the modification of the primary group is essential. In this article, we are going to deliver concise details about user groups and how to manage them. What is a Primary Group?In Linux, every user is associated with a primary group. The primary gr
7 min read
How to Change the Default Port in PostgreSQL
PostgreSQL is one of the most powerful and widely used relational database management systems (RDBMS) in the world. By default, PostgreSQL listens for incoming connections on port 5432. In this article, we will describe the process of changing the default port for PostgreSQL in detailed and step-by-
6 min read
How to set up a PostgreSQL Database with Podman
Podman is a tool that developers and system administrators are using more and more to manage and deploy their software stacks as the need for containerized applications grows. We will look at how to use Podman to set up and maintain a PostgreSQL database in this tutorial. The overview of PostgreSQL
7 min read
How to List Databases and Tables in PostgreSQL using PSQL
PostgreSQL is a powerful, open-source object-relational database system. It provides a wide array of tools and features to manage databases, tables, and other database objects. In this article, we will explain how to list databases and tables in PostgreSQL using the psql command-line interface. We w
3 min read
How To Use PostgreSQL with your Django Application on Ubuntu
This article describes how to configure PostgreSQL with the Django application on your Ubuntu machine. First, let's look at an overview of all the tools we use. PostgreSQL is a high-performance, reliable, and robust open-source relational database management system (RDBMS).Django is a robust, free,
4 min read
Run PostgreSQL on Docker and Setting Up pgAdmin
PostgreSQL, an effective tool, is a freeÂ-to-use relational database management system. Docker can quickly construct and orcheÂstrate its instances without bothering about the complexity of setup or depeÂndencies. This step-by-step simple guide will show you how to get PostgreÂSQL on Docker, and the
7 min read