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;
Answer from django-renjith on Stack OverflowChange 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;
Try:
sudo -u postgres psql -c 'create database test;'
Videos
There are two different ways of creating a database in PostgreSQL. One is from the command line. The other is from the PostgreSQL console.
Command Line
In order to be able to create a database from the command line, you must first change to a user with rights to create a database. As you have shown with the \du command above, the user with those rights is postgres.
root@eric-desktop:/home/eric# su postgres
postgres@eric-desktop:/home/eric$ createdb prismatest2
postgres@eric-desktop:/home/eric$
As you can see, the creatdb command does not give any feedback. You need to confirm the creation by listing the current databases:
psql -U postgres -l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-------------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
prismatest | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
prismatest2 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
PostgreSQL Console
Another possibility is to use the PostgreSQL console. To enter the console:
root@eric-desktop:/home/eric# sudo -u postgres psql psql (15.7 (Debian 15.7-0+deb12u1)) Type "help" for help.
postgres=#
postgres=# CREATE DATABASE "prismatest3";
CREATE DATABASE
postgres=#
To confirm the creation of the database, list the databases:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-------------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
prismatest | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
prismatest2 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
prismatest3 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | libc |
Notice how the prompt has changed from the first line to the second:
v
postgres=# createdb prismatest
postgres-# \l
^
psql is hinting that you are entering more lines of the same command.
Enter the semicolon (";") that terminates the create database statement.
postgres=# createdb prismatest ;