How to Create User in PostgreSQL Database Server

Posted on

As shown in the title of this article, the main purpose for creating user in PostgreSQL Database Server can be done or it can be carried out in a simple way. It can be done by executing specific command in the PostgreSQL Command Console. But the most important thing to be remembered is the step for creating a new user need to be preceeded with several steps. Those steps are shown as follows :

1. A running PostgreSQL Database Server. Basically, users are literally attached as the attribute  provided in order for a PostgreSQL Database Server to be accessed. Off course, the running PostgreSQL Database Server must be available to listen for incoming request.

2. An access to PostgreSQL Database Server which is specifically the access for PostgreSQL Command Console. By accessing PostgreSQL Command Console, the command which is used to create a new user can be executed. The pattern for creating a new user is sshown below :

CREATE USER user_name WITH PASSWORD 'password';

The example for creating a user based on the pattern above is shown as follows :

postgres=# CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE ROLE
postgres=#

3. Another thing which is needed to be found out after executing the above command is listing the available user using a specific command as shown below  :

user@hostname:~$ psql -Upostgres
Password for user postgres: 
psql (9.5.9, server 9.3.10)
Type "help" for help.

postgres=# \du
                                        List of roles
  Role name   |                   Attributes                   |          Member of           
--------------+------------------------------------------------+------------------------------
 myuser       |                                                | {}
 postgres     | Superuser, Create role, Create DB, Replication | {}

postgres=# select usename from pg_user;
   usename   
-------------
 postgres
 myuser
(2 rows)

postgres=#

The above output command generating different result. The first query executed in the PostgreSQL Command Console is specifically listing all available roles and users with their specific attributes. The second query is a query specifically executed to display and to list available users in PostgreSQL Database Server. So, based on the output of the query executed, user has been created successfully.

Leave a Reply