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;'
Running createdb from CLI - how does that work?
Can't create database in PostgreSQL using Git Bash in Windows 10
How do I simply navigate to a folder and create a database file there with BASH?
Just installed PostgreSQL, not able to follow step 1: $ createdb mydb
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 ;
Hi all,
Pretty new to postgres and been playing with creating and dropping db's with my CLI.
My question is: Why/how is it that I can run a command like createdb or drop database from outside of the psql repl / just in my root directory (aka: ~)?
I get that there's a connection running, but is it also listening for those commands despite the CLI being in an unrelated area?
From a GUI-perspective, isn't that like calling a command for one application while just on your desktop / the application is minimized and not active? It feels like it's hitting Ctrl+B on my desktop and expecting my Word document to bold something.
TIA!