Report this

What is the reason for this report?

postgres new user

Posted on January 22, 2025

How can i create new Superuser in postgres in par with postgres default user? I want this new user to have its own psw that i can use to access the database created



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

To create a new superuser, use the CREATE USER command along with SUPERUSER and LOGIN options, and set the password for the new user. Replace new_username with the desired username and your_password with the password you want for this new user:

CREATE USER new_username WITH SUPERUSER LOGIN PASSWORD 'your_password';

Grant privileges to the new user:

This step isn’t always necessary because the SUPERUSER role already provides full privileges, but you can explicitly grant all privileges to the new user on a specific database if needed:

GRANT ALL PRIVILEGES ON DATABASE your_database TO new_username;

Exit PostgreSQL:

Once the user has been created, exit the PostgreSQL prompt. Now you can try logging in with the new superuser account by running the following command:

psql -U new_username -d your_database -W

You’ll be prompted to enter the password for new_username to access the database.

This will create a new superuser with the specified password, allowing you to access the database using this new user.

Heya, @charmeem

You can create the superuser using CREATE ROLE

CREATE ROLE new_username WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'your_password';

You can manage privileges efficiently by creating a group role:

  1. Create a group role:
CREATE ROLE admin_group WITH SUPERUSER CREATEDB CREATEROLE NOLOGIN;
  1. Assign your new user to the group:
GRANT admin_group TO new_username;

This setup makes it easier to manage superuser-like privileges for multiple users.

Also assign superuser privileges only if absolutely necessary. For less sensitive tasks, consider roles with specific privileges instead.

Hope that this helps!

Hi there,

In addition to what has already been mentioned, if you are using a managed Postgres cluster, you can follow the steps on how to do that here:

https://docs.digitalocean.com/products/databases/postgresql/how-to/manage-users-and-databases/

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.