Postgres Create Database and user from cmd(command line)

Step 1: Change user to Postgres :

su - postgres

Step 2: Create user for Postgres

$ createuser test_user

Step 3: Create Database

$ createdb test_db

Step 4: Provide the privileges to the Postgres user

$ alter user test_user with encrypted password 'qwerty';$ grant all privileges on database test_db to test_user;

Step 5: Access the Postgres Shell

psql ( enter the password for postgressql)

Important: createuser and createdb are run in the shell and NOT with psql

Method 2: 

Step 1: Create Database

sudo -u postgres psql -c 'create database test_db;

Step 2:  Creating user

sudo -u postgres psql -c 'create user test_user'

Step 3: Don’t forget to grant permissions as well: 

sudo -u postgres psql -c 'grant all privileges on database test_db to test_user;'