2 The first method, use the PostgreSQL console.
First, create a new Linux user, you can take the name you want, here is dbuser.
sudo adduser dbuser
Then, switch to Postgres user.
sudo su-postgres
Next, log in to the PostgreSQL console using the Psql command.
Psql
This is equivalent to the system user Postgres logged in to the database as a database user with the same name, which is not a password to enter. If all goes well, the system prompt changes to "postgres=#", which means that the database console is now in. The following commands are completed within the console.
The first thing to do is to use the \password command to set a password for the Postgres user.
\password postgres
The second thing is to create a database user Dbuser (a Linux system user just created) and set the password.
CREATE USER dbuser with PASSWORD ' PASSWORD ';
The third thing is to create the user database, here exampledb, and specify the owner as Dbuser.
CREATE DATABASE exampledb OWNER dbuser;
The fourth thing is to give Dbuser all the permissions of the Exampledb database, or dbuser can only log in to the console without any database operation permissions.
GRANT all privileges on the DATABASE exampledb to Dbuser;
Finally, use the \q command to exit the console (you can also press ctrl+d directly).
\q
3The second method, use the shell command line.
Adding new users and new databases can be done under the shell command line, in addition to the PostgreSQL console. This is because PostgreSQL provides command-line programs CreateUser and Createdb. Take the new user Dbuser and database Exampledb as an example.
First, create a database user, Dbuser, and specify it as a super user.
Sudo-u postgres CreateUser--superuser dbuser
Then, log in to the database console, set the password for the Dbuser user, and exit the console when you are finished.
Sudo-u Postgres Psql
\password Dbuser
\q
Next, under the shell command line, create the database exampledb and specify the owner as Dbuser.
Sudo-u postgres createdb-o dbuser exampledb
END