Postgresql

Frequently used code for postgresql

Recipes

Installing - Ubuntu

sudo apt-get install wget ca-certificates
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-get update
sudo apt-get install postgresql postgresql-contrib

Installing - Docker

docker run --name pg_docker \
    -p 5432:5432 \
    -e POSTGRES_DB=db-name \
    -e POSTGRES_PASSWORD=pass \
    -d postgres
    
# Access from host
psql -h localhost -p 5432 -d db-name -U postgres --password

Execute commands from current user (without user on db)

echo "\d" | sudo -u postgres psql

Create database

Create user

Grant privilage to such user

Create table

List db \l

List users \du

List tables

\dt

Change user password

List constraint for a table

Create constraint

Create index

Remove constraint

Generating dump file

Loading dump file

Kill postgresql session

Useful tools

Last updated

Was this helpful?