PostgreSQL

Created
TypeDatabase
LanguageSQL
Last Edit

Startup

Macos

brew services start postgresql@14
brew services restart postgresql@14

Docker

Download Postgres Image

docker pull postgres

Create Container from Image

docker run --name postgresql -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=admin -p 5432:5432 -v Users/ashirali-mg/Documents/postgres-data -d postgres

Verify

docker ps

Get Connection Details

docker inspect postgresql

VPS Setup

Install

Installation Using Package Manger

sudo apt install postgresql postgresql-contrib

Installation From APT Repository

Run each command seperately

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

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

sudo apt-get update

sudo apt-get -y install postgresql

Test

sudo -u postgres psql
SELECT version();

Connect

sudo su postgres
psql

Connect Remote DB

psql -h hostname_or_ip -p port -d database_name -U username

Install pgAdmin on VPS

Run each command seperately

curl -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/pgadmin.gpg

sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list'

sudo apt update

sudo apt install pgadmin4

sudo /usr/pgadmin4/bin/setup-web.sh

Connect (in browser)

ip_address/pgadmin4

Create User

  1. Switch to the PostgreSQL user
sudo -i -u postgres
  1. Access the PostgreSQL interactive terminal
psql

Above two commands in one:

💡
sudo -u postgres psql
  1. Create a new PostgreSQL user
CREATE USER new_user WITH PASSWORD 'password';
  1. Create a new PostgreSQL database
CREATE DATABASE new_database;

5. You will set the default character encoding to UTF-8, which Django expects. You are also setting the default transaction isolation scheme to “read committed”, which blocks reads from uncommitted transactions. Lastly, you are setting the timezone. By default, Django projects will be set to use UTC. These are all recommendations from the Django project itself:

ALTER ROLE db_admin SET client_encoding TO 'utf8';
ALTER ROLE db_admin SET default_transaction_isolation TO 'read committed';
ALTER ROLE db_admin SET timezone TO 'UTC';
  1. Grant privileges to the new user
GRANT ALL PRIVILEGES ON DATABASE new_database TO new_user;

6.a. Manage Privileges For A Role

REVOKE ALL PRIVILEGES ON *.* FROM 'yourusername'@'%';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES ON `yourdatabase`.* TO 'yourusername'@'%';
  1. Exit From Psql
    \q

Config Entry

pg_hba.conf

Usually in

/etc/postgresql/14/main/pg_hba.conf

When edited

sudo systemctl restart postgresql

Interface

MacOS

psql -U postgres
Password: admin

Docker

docker exec -it <container_id> bash
psql -U postgres

General Queries

\l : List all databases available

\c: Select a database

\dt: Show tables in database

Create

Create Database

CREATE DATABASE pern_todo;

Create Table

CREATE TABLE todo (
    todo_id SERIAL PRIMARY KEY,
    description VARCHAR ( 255 )
);

UUID as ID

CREATE TEMP TABLE IF NOT EXISTS users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid()
);

Drop

All Tables

DROP SCHEMA public CASCADE;
CREATE SCHEMA public;

//OPTIONAL:
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;

Database

DROP DATABASE ucem_db;

Delete Trigger

Syntax:

DROP TRIGGER trigger_name ON table_name

Example:

DROP TRIGGER update_tgr ON sections;

Master & Slave

Achieving PostgreSQL Master Slave Replication: 7 Easy Steps
PostgreSQL is an extremely robust open-source database used by noted players like Skype, Reddit, Instagram, and OpenStreetMap. As the traffic on websites and applications increases the frequency of reading and writing data from the Database increases and so does the load. PostgreSQL allows replication to nodes that can run read-only queries.
https://hevodata.com/learn/postgresql-master-slave-replication/

Backup

Backup From Docker

pg_dump -U postgres -W -Fp msigma_db -f /tmp/msigma.sql
docker cp postgresql:/tmp/msigma.sql /Users/ashirali-mg/Work/Msigma

Best Practices

Nine ways to shoot yourself in the foot with PostgreSQL
Previously on Extreme Learning, I discussed all the ways I've broken production using healthchecks. In this post I'll do the same for PostgreSQL.
https://philbooth.me/blog/nine-ways-to-shoot-yourself-in-the-foot-with-postgresql