Let’s take a look at an example of renaming a database. Use the ALTER DATABASE statement to rename the database to the new one.Check and terminate all active connections to the database that you want to rename. Disconnect from the database that you want to rename and connect to a different database.To rename a PostgreSQL database, you use the following steps: This write-up explained how to rename a user/role in Postgres via the ALTER ROLE and ALTER USER statements.Summary: in this tutorial, you will learn step by step how to rename a PostgreSQL database using the ALTER DATABASE RENAME TO statement. To do that, log out from the current user, log in as another user, and execute the ALTER USER or ALTER ROLE statement with the RENAME TO clause to rename the current session user. In Postgres, the session user(currently logged in) can't be renamed. In PostgreSQL, the RENAME TO clause is used with the ALTER USER or ALTER ROLE statement to rename a user. The output snippet verifies that the “example_user” has been renamed to “user_1” successfully. You can verify the user’s modified name using the “\du” command: \du For this purpose, we will execute the “ALTER USER” statement as follows: ALTER USER example_user Suppose we need to rename the “example_user” to “user_1”. Let’s check the available users using the “\du” command: \du Let’s understand this concept practically.Įxample: Rename User Using ALTER USER Statement Next, specify the RENAME TO clause and the new username: ALTER USER user_name To rename a user via ALTER USER statement, specify the ALTER USER statement followed by the user name. How Do I Rename a User/Role in Postgres Using ALTER USER Command? The output clarifies that the “command_prompt” user has been renamed to “cp_user” successfully. Let’s verify the role’s modified name using the “\du” command: \du The above snippet verifies that the ALTER ROLE statement was executed successfully. Let’s execute the ALTER ROLE statement to rename the “command_prompt” user to “cp_user”: ALTER ROLE command_prompt In the above snippet, we are logged in as “postgres” users. To rectify this error, we must log in from some other user, as shown below: Let’s try to rename it using the ALTER ROLE statement: ALTER ROLE command_promptĪn error occurred when we tried to rename the session user. The output snippet shows that the “sample_user” has been renamed to “sample_role”.Įxample 2: Rename Currently Logged in User Let’s verify the user alteration via the “\du” command: \du For this purpose, the ALTER ROLE statement will be executed as follows: ALTER ROLE sample_user Suppose we want to rename the “sample_user” to “sample_role”. The below syntax must be followed to rename a user in Postgres: ALTER ROLE user_nameĮxample 1: Rename User Using ALTER ROLE Statementįirstly, execute the “\du” command to see the available users: \du How Do I Rename a User/Role in Postgres Using ALTER ROLE Command? This write-up will teach you how to rename a user in Postgres via ALTER ROLE and ALTER USER commands. You must log out from the current user and log in as another user to rename the current session user. PostgreSQL provides a RENAME TO clause that is used with the ALTER ROLE or the ALTER USER statements to rename a user.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |