Add new use postgres
Here are the steps to add a new user to a PostgreSQL database:
Using the createuser
command
-
Open a terminal or command prompt and navigate to the directory where your PostgreSQL database is installed.
-
Use the
createuser
command to create a new user:createuser -P <username>
Replace
<username>
with the desired username for the new user. -
Enter a password for the new user when prompted.
-
Confirm the password when prompted.
Using the psql
command
-
Open a terminal or command prompt and navigate to the directory where your PostgreSQL database is installed.
-
Use the
psql
command to connect to your PostgreSQL database:psql -U <existing_username> <database_name>
Replace
<existing_username>
with the username of an existing user with sufficient privileges, and<database_name>
with the name of the database you want to add the new user to. -
Once connected, use the following command to create a new user:
CREATE ROLE <username> WITH PASSWORD '<password>';
Replace
<username>
with the desired username for the new user, and<password>
with the desired password. -
To grant the new user privileges, use the following command:
GRANT <privileges> TO <username>;
Replace
<privileges>
with the desired privileges (e.g.SELECT
,INSERT
,UPDATE
,DELETE
, etc.), and<username>
with the new user's username.
Using the PostgreSQL GUI tool
- Open the PostgreSQL GUI tool (e.g. pgAdmin).
- Connect to your PostgreSQL database.
- Right-click on the "Roles" node in the left-hand menu and select "New Role".
- Enter the desired username and password for the new user.
- Grant the new user privileges by selecting the desired privileges from the list.
Note: The above steps assume that you have the necessary permissions to create a new user. If you are not the database administrator, you may need to ask the administrator to create the new user for you.