Skip to content

400 Software Development


postgresql

Cheatsheet: quickref

From website: https://www.cherryservers.com/blog/how-to-install-and-setup-postgresql-server-on-ubuntu-20-04

Install postgresql:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

sudo apt update  

sudo apt install postgresql postgresql-contrib

Check service is running:

service postgresql status
service postgresql start

Create DB and users

Start postgres CLI:

sudo -u postgres psql

Create/delete database:

CREATE DATABASE db_name;
DROP DATABASE db_name;

Create user:

CREATE USER username WITH PASSWORD 'password';

Connect to DB and give access to user:

\c db_name;
GRANT ALL ON SCHEMA public TO username;

Useful Commands:

\l - list all database \d - general display of information \du - display all users \dt - display all tables \c - connect to database

Postgres python drivers for developement:

pip install psycopg2-binary

(note, do not do this for production dependencies) See here why

See also