Install postgres. Create DB and user in CLI

#StandWithUkraine
Today, 20th March 2023, Ukraine is still bravely fighting for democratic values, human rights and peace in whole world. Russians ruthlessly kill all civilians in Ukraine including childs and destroy their cities. We are uniting against Putin’s invasion and violence, in support of the people in Ukraine. You can help by donating to Ukrainian's army.

On Ubuntu/Windows WSL 2 to install postgres you have to use apt package manager:

sudo apt install postgresql

Start server:

sudo service postgresql start

Change password for user postgres

sudo passwd postgres
# New password: 
# Retype new password: 
# passwd: password updated successfully

Run PostgreSQL client (psql) with user postgres privileges:

su -c psql postgres

It will ask password, enter the password you defined above.

You can always change password again if you forgot it

Create database

To create a database with name testdb type:

CREATE DATABASE testdb ENCODING 'utf-8';

Create a user for Database

CREATE USER myuser with encrypted password 'passw1';
GRANT ALL PRIVILEGES ON database testdb to myuser;
\c testdb
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser; -- don't change public
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to myuser;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to myuser;

Create postgres database

#postgresql #database
7
Ivan Borshchov profile picture
May 21, 2021
by Ivan Borshchov
Did it help you?
Yes !
No

Best related

This was very simple. I was able to run PostgreSQL in less than a minute. Great!