PostgreSQL

Online free PostgreSQL

Installing PSQL in Ubuntu

# this only install the psql client and not the database

sudo apt-get update
sudo apt-get install postgresql-client

Connecting to Remote PostgreSQL instance in psql client

psql -h <IP_Address> -p <port_no> -d <database_name> -U <DB_username> -W

-W option will prompt for password

psql -h 192.168.1.50 -p 5432 -d testdb -U testuser -W

Connecting via Docker

Install and Configure PSQL using Docker:

Run the below command in linux or windows or mac machine from the terminal or command-prompt to pull PSQL from docker-hub. docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=somePassword -d postgres In the above command replace :

  • Optional - postgresql-container with a preferable container name if necessary.

  • somePassword with a password to authenticate and connect to the postgres (in application with connection string as well as the PG-admin viewer).

Install PG-admin using Docker:

Download the pgAdmin-4 browser version from docker-hub using the following command. sudo docker run --rm -p 5050:5050 thajeztah/pgadmin4

Now manage your postgres from the browser by launching http://localhost:5050 .

Verify a new container created and running at 0.0.0.0:5432 with the below command. docker ps -a

Clean PostgreSQL terminal

Login a Postgresql database

List all the PostgreSQL user

List of All Database

Creating a user and Database

Creating a Table

Inserting Data into the table

Read the data from Table

Update the value from Table

Deleting the data from Table

Ordering in Table

LIMIT / OFFSET Clause

Datatypes

  • Char(n) - allocate the entire space (faster for small string where the lenght is known) (fixing size helps a lot) (fixe to ASCII char set)

  • Varchar(n) - allocate variables amount of space depending on the data length(less space)

  • TEXT - pararaph or HTML pages ,generally not used with indexing or sorting and only then limited to prefix

  • BYTEA(n) up to 255 bytes

  • SMALLINT (-32768,+32768)

  • INTEGER (2 Billion)

  • BIGINT (10**18 ish)

  • REAL (32 bit) 10**38 with 7 digit of approximation

  • DOUBLE PRECISION (64 bit) 10**308 with 14 digits of accuracy

  • NUMERIC (accuracy , decimal)- specified digits if accuracy and digits after decimal point

  • TIMESTAMP - ' YYYY-MM-DD HH:MM:SS"

  • DATE -" YYYY- MM- DD "

  • TIME -" HH: MM :SS "

Built in PostgreSQL functions NOW()

Database keys and Indexes in PostgreSQL

Auto increment key for as data comes in

Database decides itself what to use for indexing

  • Trees (B tree)

  • Hashes (Modulus % like) are used for Indexes

Analysing Query performance

Loading Data from SQL

Relational Database

Keys

  • Primary Key - integer auto increment field

  • Logical key - What outside world use for lookup

  • Foreign key - value point to another table

Best Practise

  • Never use logical key as primary key

  • logical keys can do change , albeit slowly

  • Relationships that are based on matching string fields are less efficient then integers

Database Normalization

3 NF

  • Do not replicate Data , instead reference data , point at data

  • use integer for keys and for references

  • Add a special "key" column to each table , which you will make references to .

Joins in SQL

One to Many Relations :

Many to Many relationship

Reference :

Video Reference :

Last updated

Was this helpful?