Search code examples
databasepostgresqlcommand-linecommandpsql

Create database from command line in PostgreSQL


I am trying to create a database from command line. My OS is centos and postgres version is 10.9.

sudo -u postgres psql createdb test
Password for user test:

Why is it prompting me for the password?


Solution

  • Change the user to postgres :

    su - postgres
    

    Create User for Postgres (in the shell and NOT with psql)

    $ createuser testuser
    

    Create Database (same)

    $ createdb testdb
    

    Acces the postgres Shell

    psql ( enter the password for postgressql)
    

    Provide the privileges to the postgres user

    $ alter user testuser with encrypted password 'qwerty';
    $ grant all privileges on database testdb to testuser;