Skip to content

most of this is copied and pasted from other people's repositories

Notifications You must be signed in to change notification settings

bgoonz/psql-practice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostgreSQL Icon

Learn PostgreSQL

A lightweight documentation of PostgreSQL for beginners.

Table of Contents


Resources:

Contents

High-Availability

  • BDR - BiDirectional Replication - a multimaster replication system for PostgreSQL
  • Patroni - Template for PostgreSQL HA with ZooKeeper or etcd.
  • Stolon - PostgreSQL HA based on Consul or etcd, with Kubernetes integration.
  • pglookout - Replication monitoring and failover daemon.
  • repmgr - Open-source tool suite to manage replication and failover in a cluster of PostgreSQL servers.
  • Slony-I - "Master to multiple slaves" replication system with cascading and failover.
  • PAF - PostgreSQL Automatic Failover: High-Availibility for Postgres, based on Pacemaker and Corosync.
  • SkyTools - Replication tools, including PgQ, a queuing system, and Londiste, a replication system a bit simpler to manage than Slony.

Backups

  • Barman - Backup and Recovery Manager for PostgreSQL by 2ndQuadrant.
  • OmniPITR - Advanced WAL File Management Tools for PostgreSQL.
  • pg_probackup – A fork of pg_arman, improved by @PostgresPro, supports incremental backups, backups from replica, multithreaded backup and restore, and anonymous backup without archive command.
  • pgBackRest - Reliable PostgreSQL Backup & Restore.
  • pg_back - pg_back is a simple backup script
  • pghoard - Backup and restore tool for cloud object stores (AWS S3, Azure, Google Cloud, OpenStack Swift).
  • wal-e - Simple Continuous Archiving for PostgreSQL to S3, Azure, or Swift by Heroku.
  • wal-g - The successor of WAL-E rewritten in Go. Currently supports cloud object storage services by AWS (S3), Google Cloud (GCS), Azure, as well as OpenStack Swift, MinIO, and file system storages. Supports block-level incremental backups, offloading backup tasks to a standby server, provides parallelization and throttling options. In addition to Postgres, WAL-G can be used for MySQL and MongoDB databases.
  • pitrery - pitrery is a set of Bash scripts to manage Point In Time Recovery (PITR) backups for PostgreSQL.

GUI

  • Adminer - Full-featured database management tool written in PHP.
  • Beekeeper Studio - Free and open source SQL client with a modern UI and great Postgres support. Cross platform.
  • DataGrip - IDE with advanced tool sets and good cross-platform experience (Commercial Software).
  • Datazenit - Web-based PostgreSQL GUI (Commercial Software).
  • DataRow - Cross-platform SQL Client for Amazon Redshift: Simple, Effortless, Extensible.
  • DBeaver - Universal Database Manager with excellent support for PostgreSQL.
  • dbglass - Cross-platform desktop client for PostgreSQL, built with Electron.
  • Holistics - Online cross platform database management tool and SQL query reporting GUI with strong PostgreSQL support (Commercial Software).
  • JackDB - Web-based SQL query interface (Commercial Software).
  • Metabase - Simple dashboards, charts and query tool for PostgreSQL.
  • Numeracy - Fast SQL editor with charts and dashboards for PostgreSQL (Commercial Software).
  • OmniDB - Open Source Collaborative Environment For Database Management
  • pgAdmin - PostgreSQL Administration and Management GUI.
  • pgModeler - pgModeler is an open-source PostgreSQL Database Modeler.
  • pgweb - Web-based PostgreSQL database browser written in Go.
  • phpPgAdmin - The Premier Web Based Administration Tool for PostgreSQL.
  • Postbird - PostgreSQL Client for macOS.
  • PostgresCompare - Cross-platform database comparison and deployment tool (Commercial Software).
  • Postico - Modern PostgreSQL Client for macOS (Commercial Software).
  • PSequel - Clean and simple interface to perform common PostgreSQL tasks quickly (Commercial Software).
  • SQL Tabs - Cross Platform Desktop Client for PostgreSQL written in JS.
  • SQLPro for Postgres - Simple, powerful PostgreSQL manager for macOS (Commercial Software).
  • temBoard - Web-based PostgreSQL GUI and monitoring.
  • TablePlus - Native App which let you edit database and structure. High-end security ensured (Commercial Software).
  • Valentina Studio - Cross-platform database administration tool (Free/Commercial)

Distributions

  • Postgres.app - The Easiest Way to Get Started with PostgreSQL on macOS.
  • PostgreSql.Binaries.Lite - Minimum set of Windows binaries of the PostgreSQL database. Also made available through NuGet.

CLI

  • pgcli - Postgres CLI with autocompletion and syntax highlighting
  • pgsh - Branch your PostgreSQL Database like Git
  • psql - The built-in PostgreSQL CLI client
  • psql2csv - Run a query in psql and output the result as CSV
  • nancy - The Nancy CLI is a unified way to manage automated database experiments either in clouds or on-premise
  • schemaspy - SchemaSpy is a JAVA JDBC-compliant tool for generating your database to HTML documentation, including Entity Relationship diagrams

Server

  • Postgres-XL - Scalable Open Source PostgreSQL-based Database Cluster.
  • AgensGraph - Powerful graph database based on the PostgreSQL.
  • Greenplum Database - Open source fork of PostgreSQL for large data volumes.

Monitoring

  • check_pgactivity - check_pgactivity is designed to monitor PostgreSQL clusters from Nagios. It offers many options to measure and monitor useful performance metrics.
  • Check_postgres - Nagios check_postgres plugin for checking status of PostgreSQL databases.
  • Instrumental - Real-time performance monitoring, including pre-made graphs for ease of setup (Commercial Software)
  • libzbxpgsql - Comprehensive PostgreSQL monitoring module for Zabbix.
  • PMM - Percona Monitoring and Management (PMM) is a Free and Open Source platform for monitoring and managing PostgreSQL, MySQL, and MongoDB.
  • Pome - Pome stands for PostgreSQL Metrics. Pome is a PostgreSQL Metrics Dashboard to keep track of the health of your database.
  • pgmetrics - pgmetrics is an open-source, zero-dependency, single-binary tool that can collect a lot of information and statistics from a running PostgreSQL server and display it in easy-to-read text format or export it as JSON and CSV for scripting.
  • pg_view - Open-source command-line tool that shows global system stats, per-partition information, memory stats and other information.
  • pgwatch2 - Flexible and easy to get started PostgreSQL metrics monitor focusing on Grafana dashboards.
  • pgbench - Run a benchmark test on PostgreSQL.
  • opm.io - Open PostgreSQL Monitoring is a free software suite designed to help you manage your PostgreSQL servers. It can gather stats, display dashboards and send warnings when something goes wrong.
  • okmeter.io - Commercial SaaS agent-based monitoring with a very detailed PostgreSQL plugin. It automatically gathers 100s of stats, displays dashboards on every aspect and sends alerts when something goes wrong (Commercial Software).

Extensions

  • Citus - Scalable PostgreSQL cluster for real-time workloads.
  • cstore_fdw - Columnar store for analytics with PostgreSQL.
  • cyanaudit - Cyan Audit provides in-database logging of all DML activity on a column-by-column basis.
  • pg_cron - Run periodic jobs in PostgreSQL.
  • pglogical - Extension that provides logical streaming replication.
  • pg_partman - Partition management extension for PostgreSQL.
  • pg_paxos - Basic implementation of Paxos and Paxos-based table replication for a cluster of PostgreSQL nodes.
  • pg_shard - Extension to scale out real-time reads and writes.
  • PGStrom - Extension to offload CPU intensive workloads to GPU.
  • pgxn PostgreSQL Extension Network - central distribution point for many open-source PostgreSQL extensions
  • PipelineDB - A PostgreSQL extension that runs SQL queries continuously on streams, incrementally storing results in tables.
  • plpgsql_check - Extension that allows to check plpgsql source code.
  • PostGIS - Spatial and Geographic objects for PostgreSQL.
  • PG_Themis - Postgres binding as extension for crypto library Themis, providing various security services on PgSQL's side.
  • zomboDB - Extension that enables efficient full-text searching via the use of indexes backed by Elasticsearch.
  • pgMemento - Provides an audit trail for your data inside a PostgreSQL database using triggers and server-side functions written in PL/pgSQL.
  • TimescaleDB - Open-source time-series database fully compatible with Postgres, distributed as extension
  • pgTAP - Database testing framework for Postgres
  • HypoPG - HypoPG provides hypothetical/virtual indexes feature.
  • pgRouting - pgRouting extends the PostGIS/PostgreSQL geospatial database to provide geospatial routing and other network analysis functionality.

Optimization

  • pg_flame - A flamegraph generator for query plans.
  • PgHero - PostgreSQL insights made easy.
  • pgMustard - A modern user interface for EXPLAIN, that also provides performance tips (Commercial Software).
  • pgtune - PostgreSQL configuration wizard.
  • pgtune - Online version of PostgreSQL configuration wizard.
  • pgconfig.org - PostgreSQL Online Configuration Tool (also based on pgtune).
  • PoWA - PostgreSQL Workload Analyzer gathers performance stats and provides real-time charts and graphs to help monitor and tune your PostgreSQL servers.
  • pg_web_stats - Web UI to view pg_stat_statements.
  • TimescaleDB Tune - a program for tuning a TimescaleDB database to perform its best based on the host's resources such as memory and number of CPUs.

Utilities

  • apgdiff - Compares two database dump files and creates output with DDL statements that can be used to update old database schema to new one.
  • ERAlchemy - ERAlchemy generates Entity Relation (ER) diagram from databases.
  • Hasura GraphQL Engine - Blazing fast, instant realtime GraphQL APIs on Postgres with fine grained access control, also trigger webhooks on database events.
  • ldap2pg - Synchronize roles and privileges from YML and LDAP.
  • mysql-postgresql-converter - Lanyrd's MySQL to PostgreSQL conversion script.
  • ora2pg - Perl module to export an Oracle database schema to a PostgreSQL compatible schema.
  • pg_activity - top like application for PostgreSQL server activity monitoring.
  • pg-formatter - A PostgreSQL SQL syntax beautifier (Node.js).
  • pganalyze - PostgreSQL Performance Monitoring (Commercial Software).
  • pgbadger - Fast PostgreSQL Log Analyzer.
  • PgBouncer - Lightweight connection pooler for PostgreSQL.
  • pgCenter - Provides convenient interface to various statistics, management task, reloading services, viewing log files and canceling or terminating database backends.
  • pg_chameleon - Real time replica from MySQL to PostgreSQL with optional type override migration and migration capabilities.
  • pgclimb - Export data from PostgreSQL into different data formats.
  • pg_docs_bot - Browser extension to redirect PostgreSQL docs links to the current version.
  • pgfutter - Import CSV and JSON into PostgreSQL the easy way.
  • PGInsight - CLI tool to easily dig deep inside your PostgreSQL database.
  • pg_insights - Convenient SQL for monitoring Postgres database health.
  • pgloader - Loads data into PostgreSQL using the COPY streaming protocol, and does so with separate threads for reading and writing data.
  • pgpool-II - Middleware that provides connection pooling, replication, load balancing and limiting exceeding connections.
  • pgsync - Tool to sync PostgreSQL data to your local machine.
  • PGXN client - Command line tool to interact with the PostgreSQL Extension Network
  • postgresql-metrics - Tool that extracts and provides metrics for your PostgreSQL database.
  • PostgREST - Serves a fully RESTful API from any existing PostgreSQL database.
  • pREST - Serve a RESTful API from any PostgreSQL database (Golang)
  • PostGraphile - Instant GraphQL API or GraphQL schema for your PostgreSQL database
  • yoke - PostgreSQL high-availability cluster with auto-failover and automated cluster recovery.
  • pglistend - A lightweight PostgresSQL LISTEN/NOTIFY daemon built on top of node-postgres.
  • ZSON - PostgreSQL extension for transparent JSONB compression
  • pg_bulkload - It's a high speed data loading utility for PostgreSQL.
  • pg_migrate - Manage PostgreSQL codebases and make VCS simple.
  • sqitch - Tool for managing versioned schema deployment
  • pgmigrate - CLI tool to evolve schema migrations, developed by Yandex.
  • pgcmp - Tool to compare database schemas, with capability to accept some persistent differences
  • pg-differ - Tool for easy initialization / updating of the structure of PostgreSQL tables, migration alternative (Node.js).
  • sqlcheck - Automatically detects common SQL anti-patterns. Such anti-patterns often slow down queries. Addressing them will, therefore, help accelerate queries.
  • postgres-checkup - a new-generation diagnostics tool that allows users to collect deep analysis of the health of a Postgres database.
  • ScaffoldHub.io - Generate fullstack PostgreSQL apps with Angular, Vue or React (Commercial Software).

Language bindings

PaaS (PostgreSQL as a Service)

  • Aiven PostgreSQL - PostgreSQL as a service in AWS, Azure, DigitalOcean, Google Cloud and UpCloud; plans range from $19/month single node instances to large highly-available setups, free trial for two weeks.
  • Amazon RDS for PostgreSQL - Amazon Relational Database Service (RDS) for PostgreSQL
  • Azure Database for PostgreSQL - Azure Database for PostgreSQL provides fully managed, enterprise-ready community PostgreSQL database as a service. It provides builtin HA, elastic scaling and native integration with Azure ecosystem.
  • Citus Cloud - Production grade scaled out PostgreSQL as a service enabling real-time workloads and sharding your multi-tenant apps.
  • Compose - PostgreSQL as a service in AWS, Google Cloud Platform, and IBM Cloud; plans range from $17.5/month for 1GB storage and scale at $12/GB beyond that. Free trial for 30 days available.
  • Database Labs - Get a production-ready cloud PostgreSQL server in minutes, from $20 a month Backups, monitoring, patches, and 24/7 tech support all included.
  • DigitalOcean Managed Databases - Fully managed PostgreSQL databases. No free plan. Starting at $15/mo. Daily backups with point-in-time recovery. Standby nodes with auto-failover.
  • ElephantSQL - Offers databases ranging from shared servers for smaller projects and proof of concepts, up to enterprise grade multi server setups. Has free plan for up to 5 DBs, 20 MB each.
  • Google Cloud SQL for PostgreSQL - Fully-managed database service that makes it easy to set up, maintain, manage, and administer your PostgreSQL relational databases on Google Cloud Platform.
  • Heroku Postgres - Plans from free to huge, operated by PostgreSQL experts. Does not require running your app on Heroku. Free plan includes 10,000 rows, 20 connections, up to two backups, and has PostGIS support.
  • Scaleway Managed Database - Fully managed PostgreSQL databases with HA, scaling, and automated backups, hosted in the EU. Starting at €10 per month.

Docker images

  • citusdata/citus - Citus official images with citus extensions. Based on the official Postgres container.
  • mdillon/postgis - PostGIS 2.3 on Postgres 9. Based on the official Postgres container.
  • postgres - Official postgres container (from Docker)

Resources

Tutorials

Blogs

Articles

Documentation

  • Wiki - user documentation, how-tos, and tips 'n' tricks

Newsletters

  • Postgres Weekly - Weekly newsletter that contains articles, news, and repos relevant to PostgreSQL.

Videos

Community

  • Mailing lists - Official mailing lists for Postgres for support, outreach, and more. One of the primary channels of communication in the Postgres community.
  • Reddit - A reddit community for PostgreSQL users with over 12000 users
  • Slack - Slack channel for Postgres with over 7000 users
  • Telegram - Several groups for PostgreSQL in different langauges: Russian >4200 people, Brazilian Portuguese >2300 people, Indonesian ~1000 people, English >750 people
  • #postgresql on Freenode - The most popular IRC channel about Postgres on Freenode with over 1000 users

What is a Database?

The database is an organized collection of structured data that is used to store, manipulate, and retrieve data.

What is SQL and Relational Database?

A relational database is a type of database that stores access to data points that are related to one another.

SQL (Structured Query Language) is a domain-specific language in programming that can manipulate a relational database.

What is PostgreSQL?

PostgreSQL also known as 'Postgres' is an open-source, robust, high-performance object-relational database system that extends the SQL language with a lot of great features that safely store and scale the most complicated data workloads.

Getting Started

Setup & Install PostgreSQL

Just follow this link Linux downloads (Ubuntu) and copy the script then paste on your terminal to install on Ubuntu using the apt repository.

For other systems follow this official link Downloads to download and for the installation instructions.

PostgreSQL Roles and User Login

PostgreSQL role is a feature by PostgreSQL for handling authentication and authorization. It's a concept of managing permissions where more than one roles can be created and roles can be members of other roles, allowing them to take on the permission to manipulate changes.

Accessing with Switching Accounts

After installation by default PostgreSQL created an account called postgres that is the default PostgreSQL Role. To log in this account, switch over to the postgres type this command:

$ sudo -i -u postgres

PostgreSQL Icon

To access a Postgres prompt type:

$ psql

PostgreSQL Icon

Now you will be logged in and able to start the interaction with the database.

To quit from Postgres prompt just type this command:

postgres=# \q

PostgreSQL Icon

Accessing without Switching Accounts

Following is the command that will log into Postgres without the intermediary bash shell in between. That means this will redirect to you on the psql command prompt:

$ sudo -u postgres psql

PostgreSQL Icon

How to check Roles in PostgreSQL

To check the list of roles first type the following command:

psql

then type this:

\du

PostgreSQL Icon

We will see the list of roles with their name, list of attributes, and member of which role.

Create roles in PostgreSQL

We noticed that by default there is a role named postgres. We can also create a new roll

from the Postgres prompt.

First, enter into the prompt by typing psql if you are not already in.

Then we can create a role simply typing this:

CREATE ROLE new_roll_name;

PostgreSQL Icon

And we can add a password for the login session and add attributes that will define which types of access the role has been authorized to manipulate into the database. Then we can also add to the column 'member of' if the role is under any member.

Let's add a role where the user name is SubAdminwith authorized as SUPERUSER, CREATEDBattributes and PASSWORDis 'abcd':

CREATE ROLE SubAdmin WITH SUPERUSER CREATEDB PASSWORD 'abcd';

PostgreSQL Icon

Type \du to see the newly created role.

PostgreSQL Icon

Update and Delete a Role in PostgreSQL

Suppose we need to add more attributes to our new role lib10 and that means we need to update the role attributes change the password.

To update attributes to the new role and change it's password type the command:

ALTER USER WITH SUPERUSER CREATEDB CREATEROLE LOGIN ENCRYPTED PASSWORD 'Newabcd';

PostgreSQL Icon

If we need to delete a role than simply type:

DROP ROLE SubAdmin;

PostgreSQL Icon

Create and Delete Roles from using shell commands (Without connecting psql)

To create a database role with non-superuser access from the shell just type and enter:

createuser -PE RoleName

PostgreSQL Icon

Where our -P flag prompts to set a password for the new role and the -Eflag indicates to store the password as an MD5-encrypted string.

We can check the role created by connecting to psqland running \du command.

To Delete a database role from the shell just type:

dropuser -i RoleName

PostgreSQL Icon

To create a superuser from the shell type:

createuser -sPE SuperuserName

PostgreSQL Icon

How to create database

There are two ways to create a database.

To create a database from shell type:

$ createdb databaseName1

PostgreSQL Icon

To create a database from psqltype:

CREATE DATABASE databaseName2;

PostgreSQL Icon

To see the list of databases and their details type from psql:

\l

PostgreSQL Icon

To delete a database from the shell type:

$ dropdb databaseName1

PostgreSQL Icon

And to delete the database from the psql type this:

DROP DATABASE databaseName2;

PostgreSQL Icon

How to connect a database

To connect a database type from the shell:

psql databasename;

PostgreSQL Icon

To switch from another database type:

\c databasename2

PostgreSQL Icon

To get help for various SQL commands type:

\h

PostgreSQL Icon

To quit from the database type:

\q

The SQL Language

To store, retrieve, and manipulate database PostgreSQL uses the basic SQL language. Let's see some examples.

Creating a new table to a database

In SQL to create a table, we need to define the table name, column name, and their data types.

To create a table type copy this:

CREATE TABLE weather (
    city            varchar(80),
    temp_lo         int,           -- low temperature
    temp_hi         int,           -- high temperature
    prcp            real,          -- precipitation
    date            date
);

PostgreSQL Icon

Here we can see a table named weather where 5 columns and their data types added. Here in the first column city is a column and varchar is its types with a range of 80 characters. We can add a comment to start with-- and we can see three comments here.

Another example:

CREATE TABLE cities (
    name            varchar(80),
    location        point
);

PostgreSQL Icon

The point type is an example of a PostgreSQL specific data type.

Deleting a table from a database

To delete a database type this:

DROP TABLE tablename;

Inserting Data Into The Table

To add or insert data into a table type this example:

INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');

PostgreSQL Icon

Here we can see the table name should be specified with its columns value.

Another example:

INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');

PostgreSQL Icon

We saw a type PostgreSQL specific data type point above example shows that we the of point type should be defined as coordinate pairs.

An alternative way to insert data to avoid remembering the column name:

INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
    VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');

PostgreSQL Icon

The column name can be listed in a different order.

INSERT INTO weather (date, city, temp_hi, temp_lo)
    VALUES ('1994-11-29', 'Hayward', 54, 37);

PostgreSQL Icon

An additional feature and optimized way to insert a large number of data just type COPYcommand with the table name and its location where the database exists.

COPY weather FROM '/location/fileName.txt';

Querying data from the table

To retrieve data from a table type this:

SELECT * FROM weather;

PostgreSQL Icon

We can write expressions, not just simple column references, in the select list. For example, we can do:

SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

PostgreSQL Icon

Deleting Data Into The Table

To delete a certain record we will use DELETE and WHERE. By using WHERE clause we are giving a certain condition which record to delete. We will discuss about WHERE more in upcoming topics.

Type this to delete the third row from the table weather:

DELETE FROM weather WHERE temp_lo = 37;

PostgreSQL Icon

Now let's check the table by typing this:

SELECT * FROM weather;

PostgreSQL Icon

Creating a Table With Constraints

We can specify some constraints to a table where the constraints need to be satisfied to insert data into the table.

Let's create a table with some constraints where all column has a constraint NOT NULL except emailcolumn. Type this:

CREATE TABLE person1 (
	id BIGSERIAL NOT NULL PRIMARY KEY,
	first_name VARCHAR(50) NOT NULL,
	last_name VARCHAR(50) NOT NULL,
	email VARCHAR(50),
	gender VARCHAR(5) NOT NULL,
	data_of_birth DATE NOT NULL
);

The constraint NOT NULL specifies that the column must have a data, it can not be left as blank.

PostgreSQL Icon

Let's check and see the description of the table by typing:

\d person

PostgreSQL Icon

Creating a table and Inserting Into that table from a SQL file

Let's assume that we have a SQL file where we have a table named person and it's column values in a location named /filelocation/person.sql. Now, we need to run that file from PostgreSQL prompt, then type:

\i /filelocation/person.sql

PostgreSQL Icon

To retrieve all data from the table type:

SELECT * FROM person;

PostgreSQL Icon

Sort Data by Using 'Order BY'

In SQL we can retrieve sorted data of a table by using ORDER BY command. Let's try to retrieve the sorted data by ascending order from our previous table by their country_of_birth. Type the following SQL Command.

SELECT * FROM person ORDER BY country_of_birth ASC;

PostgreSQL Icon

Notice above the table where the country_of_birth is sorted by Ascending Order.

To sort the column by descending order type:

SELECT * FROM person ORDER BY country_of_birth DESC;

PostgreSQL Icon

Now, notice above the table where the country_of_birth is sorted by Descending Order.

Like these, we can sort the table by the rest of the column names. (id, first_name, last_name, email, date_of_birth)

Now, Let's sort the table by multiple column names. Type this:

SELECT * FROM person ORDER BY first_name, email;

PostgreSQL Icon

The output of the command is showing that first_name and email columns are sorted in Ascending Order.

Taking only one column and sort that column in Ascending Order type:

SELECT last_name FROM person ORDER BY last_name ASC;

PostgreSQL Icon

To retrieve unique data of a column we will use DISTINCT command.

Let's assume that we need to retrieve the unique country_of_birth which are appeared only once on the table person let's type:

SELECT DISTINCT first_name  FROM person ORDER BY first_name;

Following image is showing the output:

PostgreSQL Icon

Uses of WHERE Clause, AND and OR in PostgreSQL

To retrieve data of a specific group such as gender = Female from the table person type this:

SELECT * FROM person WHERE gender = 'Female';

PostgreSQL Icon

We can use multiple conditions in WHERE clause using AND conditional operator.

Let's retrieve data of male gender from country Poland, type:

SELECT * FROM person WHERE gender = 'Male' AND country_of_birth = 'Poland';

PostgreSQL Icon

Now, we will use the OR condition to retrieve data from more than one country. For showing male gender from Poland and Bangladesh type this:

SELECT * FROM person WHERE gender = 'Male' AND country_of_birth = 'Poland' OR country_of_birth = 'Bangladesh';

PostgreSQL Icon

Using Comparison Operators in PostgreSQL

In SQL this is allowed to use some types of operations and they are:

  • Arithmetic Operations
  • Comparison Operations
  • Bitwise Operations
  • Logical Operations

Following are some comparison operations performing on psql :

PostgreSQL Icon

Here, t = true and, f = false.

We can also perform these operations on other data types. Let's see how to perform on string:

PostgreSQL Icon

Using LIMIT, OFFSET & FETCH keywords

LIMIT is used to retrieve specific numbers of data from data. Let's assume that we want to see only the first 10 rows from the person table, then type this:

SELECT * FROM person LIMIT 10;

PostgreSQL Icon

OFFSET keyword is used when we need to skip a certain amount of data and start from where want to see.

Let's assume that, we want to retrieve and show 10 rows of data skipping the first 5 rows from the table, then type this:

SELECT * FROM person OFFSET 5 LIMIT 10;

PostgreSQL Icon

Here above image, you can see the id column started from 6 and showing only 10 rows till id is 15.

FETCH keyword allows the same thing as LIMIT but LIMIT is not a standard SQL keyword where FETCH is an actual standard keyword in SQL.

Let's see an example, type this:

SELECT * FROM person OFFSET 5 FETCH FIRST 5 ROW ONLY;

PostgreSQL Icon

Using of IN Keyword

This keyword is used for showing specific data related to specific values.

Let's assume that we want to show data only from country_of_birth Poland, Brazil, France, then type this:

SELECT * FROM person WHERE country_of_birth IN ('Poland', 'Brazil', 'France');

PostgreSQL Icon

Using of BETWEEN Keyword

This keyword is used to select data from a range. To find persons in a specific range of date_of_birth type:

SELECT * FROM person WHERE date_of_birth BETWEEN DATE '2019-01-01' AND '2019-10-01';

PostgreSQL Icon

Using of LIKE and ILIKE operators

Let's retrieve data using LIKE of email addresses of .org the domain from email, type this:

SELECT * FROM person WHERE email LIKE '%.org';

PostgreSQL Icon

We can specify at least how many characters will appear before .org

For example, we want only those email addresses which have at least 17 characters long before.org then we need to add 17 numbers of '_' (dash) before the .org domain by typing like this:

SELECT * FROM person WHERE email LIKE '%_________________.org';

PostgreSQL Icon

ILIKE is used to ignore the case sensitive issues. Such as, we will type same SQL command replacing LIKE with ILIKE exactly before we used where we will type .org as a capital case .ORG and it will show the exact same output.

SELECT * FROM person WHERE email ILIKE '%_________________.ORG';

PostgreSQL Icon

Using GROUP BY Keyword

This is used for grouping by the basis of columns of a table. Let's see an example.

Suppose, we need to calculate the statistics of how many people we have from each of the countries on the table. Then type this:

SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth;

PostgreSQL Icon

We can use HAVING keyword with GRPOP BY keyword to specify a condition.

Let's suppose that we want only those country_of_birth which have more than 5 persons on the table. Then type:

SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth HAVING COUNT(*) > 5 ORDER BY country_of_birth;

PostgreSQL Icon

We used COUNT() in previous examples which were an Aggregate Function. This function is used for calculating the number of values of a column. There are more aggregate functions like SUM() - calculate the summation of the values of a column, AVG() - gives an average of the values of a column, MIN() - gives the minimum value of a column, MAX() - gives the maximum value of a column, etc.

Before using these Aggregate Functions we will create a table and insert into that table from another SQL file named car.sql:

\i /filelocation/car.sql

PostgreSQL Icon

To get the maximum, minimum, average and sum value of price type:

PostgreSQL Icon

Using Arithmetic Operations

To perform arithmetic operations have a look following the image:

PostgreSQL Icon

To perform 10% OFF into the price of the car, let's type a query:

SELECT id, make, model, price AS Original_Price, round(price * .10, 2) AS Ten_Percent, ROUND(price - (price) * .10) AS After_Discount FROM car;

Here, ROUND() is a function to make the required figure of a number. Then AS is an Alias that uses to set the column names.

PostgreSQL Icon

How to Handle NULL Values in PostgreSQL

Uses of COALESCE

When a column has some NULL values and we want to set those values by a default value without leaving them totally blank, we can use COALESCE in PostgreSQL.

Let's see how it actually looks before and after using COALESCE when selecting data from a table.

Before using COALESCE:

SELECT COALESCE(email) FROM person;

PostgreSQL Icon

After using COALESCE

SELECT COALESCE(email, 'Email Not Provided.') FROM person;

PostgreSQL Icon

Uses of NULLIF

This is used to handle division by zero.

Have a look on the following screen shot:

PostgreSQL Icon

We know that in arithmetic a numeric value can not be divided by Zero. So as usual Postgres

raises error while we try to perform division operation by Zero. So in that case we use the NULLIF keyword where two parameters are used, the first one is considered to divide the given value only if the value is greater than or equal to the second value.

It doesn't show the result while both parameters are zero, meanwhile, we can use COALESCE to set a default value that will show a 'Zero'

Timestamps and Dates in PostgreSQL

Let's see some date and times operations on terminal:

PostgreSQL Icon

Adding and Subtracting With Date

We can add or subtract with dates in PostgreSQL using the INTERVAL keyword.

Let's try to subtract '1 YEAR', '10 YEAR', '10 MONTHS', '10 DAYS' from current dates. Type:

SELECT NOW() - INTERVAL '1 YEAR';
SELECT NOW() - INTERVAL '10 YEAR';
SELECT NOW() - INTERVAL '10 MONTHS';
SELECT NOW() - INTERVAL '10 DAY';

PostgreSQL Icon

To add '1 YEAR', '10 YEAR', '10 MONTHS', '10 DAYS' from current dates. Type:

SELECT NOW() + INTERVAL '1 YEAR';
SELECT NOW() + INTERVAL '10 YEAR';
SELECT NOW() + INTERVAL '10 MONTHS';
SELECT NOW() + INTERVAL '10 DAY';

PostgreSQL Icon

If we want to cast the only date and ignore the time, type:

SELECT (NOW() + INTERVAL '10 YEAR')::DATE;

PostgreSQL Icon

Extracting Field From Date, Times in PostgreSQL

We can extract single value such as Year, Month, Date, Minutes, etc. from dates and times by using EXTRACT keyword.

Let's walk through some example on terminal:

PostgreSQL Icon

Using AGE() Function To Calculate Age

Let's calculate the age of all persons using AGE() function and date_of_birth column from our previous person table:

Type this:

SELECT first_name, last_name, gender,country_of_birth, AGE(NOW(), date_of_birth) AS age FROM person;

PostgreSQL Icon

Primary Key In PostgreSQL

We use the PRIMARY KEY to differ from the same values of a column or record. Such as, we can use a unique id to identify them as individuals where two person's first_name, last_name, and date_of_birth are the same in a table.

PostgreSQL Icon

Understanding Primary Keys

If we notice in the description of the table person then we can see that the table has already a constraint name 'person_pkey' for column id which is the PRIMARY KEY of the table. Type:

\d person

PostgreSQL Icon

Now type the following query to see the first row of the table:

SELECT * FROM person LIMIT 1;

PostgreSQL Icon

Now if we insert again the same record as row number 1 to the table Postgres will raise an error that will say.

ERROR: duplicate key value violates unique constraint "person_pkey" DETAIL: Key (id)=(1) already exists.

Let's try to insert same records into the table:

insert into person (id, first_name, last_name, email, gender, date_of_birth, country_of_birth) values (1, 'Dennis', 'Fritche', null, 'Male', '01-May-2020', 'Indonesia');

PostgreSQL Icon

Now, let's Drop the CONSTRAINT from the table:

ALTER TABLE person DROP CONSTRAINT person_pkey;
ALTER TABLE

PostgreSQL Icon

Notice at the below image where we can see the CONSTRAINT 'person_pkey' is dropped and there is no field that contains PRIMARY KEY.

PostgreSQL Icon

Now let's try to insert same records again, and this time the record will be inserted into the table:

insert into person (id, first_name, last_name, email, gender, date_of_birth, country_of_birth) values (1, 'Dennis', 'Fritche', null, 'Male', '01-May-2020', 'Indonesia');

PostgreSQL Icon

Type the following command to see the inserted record into the table:

SELECT * FROM person WHERE id = 1;

PostgreSQL Icon

Adding Primary Key

In the previous topic, we dropped the CONSTRAINT from the person table and there is no PRIMARY KEY on the table person.

Now let's add PRIMARY KEY to that table by typing:

ALTER TABLE person ADD PRIMARY KEY(id);

And This will raise an error like this:

PostgreSQL Icon

Because the field needs to be unique to set it PRIMARY KEY but there is already two exactly the same records which we inserted into the table.

So first let's remove the duplicate records by typing this:

DELETE FROM person WHERE id = 1;

PostgreSQL Icon

Now, Insert the deleted unique record into the table:

insert into person (id, first_name, last_name, email, gender, date_of_birth, country_of_birth) values (1, 'Dennis', 'Fritche', null, 'Male', '01-May-2020', 'Indonesia');

PostgreSQL Icon

This time we can add the PRIMARY KEY into the table. Type this:

ALTER TABLE person ADD PRIMARY KEY(id);

PostgreSQL Icon

Let's check the CONSTRAINT of the table person by typing:

\d person

and we will notice the PRIMARY KEY is added.

PostgreSQL Icon

UNIQUE CONSTRAINTS

To set other fields or columns allowed for the only unique value that means there can not be any duplicate value in a column, we can use UNIQUE CONSTRAINT.

Let's make email column as UNIQUE constraint, type this:

ALTER TABLE person ADD CONSTRAINT unique_email_address UNIQUE (email);

To check if the CONSTRAINT is added let's type:

\d person

PostgreSQL Icon

Now we can not insert the same email value into table person that email address already contains any row from the table.

CHECK CONSTRAINTS

Another CONSTRAINT is used that checks values from a certain condition. If want to we add this constraint to the gender column of the table person then type:

ALTER TABLE person ADD CONSTRAINT gender_constraint CHECK (gender = 'Female' OR gender = 'Male');

Here we set a CONSTRAINT that checks the inserted gender is whether Male or Female. That means we can not insert a record that contains any other string rather than 'Male' or 'Female',

Type \d person to check the CONSTRAINTS from the indexes:

PostgreSQL Icon

Updating Records in PostgreSQL

UPDATE command SQL allows us to update a column or multiple columns using WHERE clause.

Let's update a row where id is 901 and we will replace the email address with a new mail address:

First let's see the id number 901 with SELECT command:

SELECT * FROM person WHERE id = 901;

PostgreSQL Icon

Then type the following command to update the mail address replacing by [email protected].

UPDATE person SET email = '[email protected]' WHERE id = 901;

PostgreSQL Icon

To show the Output:

SELECT * FROM person WHERE id = 901;

PostgreSQL Icon

To update multiple column such as, we will update the first_name and last_name of id number 901. Let's Do it:

UPDATE person SET first_name = 'Scarlett', last_name = 'Johansson' WHERE id = 901;

PostgreSQL Icon

SELECT * FROM person WHERE id = 901;

PostgreSQL Icon

On Conflict Do Nothing

To handle duplicate key error we can use ON CONFLICT command. Let's work through on terminal:

PostgreSQL Icon

To ignore this error add at the end of the insert command before the semicolon end:

ON CONFLICT(id) DO NOTHING;

PostgreSQL Icon

Note: It will only work for the Unique column, here id and email are containing unique values.

ON CONFLICT DO UPDATE

This keyword does similarly what our previous UPDATE command was used for. Let's try to update the country_of_birth column of id number 1 of the person table.

`SELECT * FROM person WHERE id = 1;`

PostgreSQL Icon

We are going to change the country_of_birth from 'Indonesia' to 'Finland':

insert into person (id, first_name, last_name, email, gender, date_of_birth, country_of_birth) values (1, 'Dennis', 'Fritche', null, 'Male', '01-May-2020', 'Indonesia')
ON CONFLICT (id) DO UPDATE SET country_of_birth = 'Finland';

PostgreSQL Icon

SELECT * FROM person WHERE id = 1;

PostgreSQL Icon

What Is A Relationship/Foreign Keys

A foreign key refers to a column or a group of columns in a table that references the primary key of another table.

Let's see an example of creating two table names customers and contacts. In this example, the customers table is the parent table and the contacts table is the child table. [ i ]

Each customer has zero or many contacts and each contact belongs to zero or one customer.

CREATE TABLE customers(
   customer_id INT GENERATED ALWAYS AS IDENTITY,
   customer_name VARCHAR(255) NOT NULL,
   PRIMARY KEY(customer_id)
);

CREATE TABLE contacts(
   contact_id INT GENERATED ALWAYS AS IDENTITY,
   customer_id INT,
   contact_name VARCHAR(255) NOT NULL,
   phone VARCHAR(15),
   email VARCHAR(100),
   PRIMARY KEY(contact_id),
   CONSTRAINT fk_customer
      FOREIGN KEY(customer_id)
	  REFERENCES customers(customer_id)
);

The following foreign key constraint fk_customer in the contacts table defines the customer_id as the foreign key:

     CONSTRAINT fk_customer
   FOREIGN KEY(customer_id)
      REFERENCES customers(customer_id)

The following inserts data into the customers and contacts tables:

INSERT INTO customers(customer_name)
VALUES('BlueBird Inc'),
      ('Dolphin LLC');

INSERT INTO contacts(customer_id, contact_name, phone, email)
VALUES(1,'John Doe','(408)-111-1234','[email protected]'),
      (1,'Jane Doe','(408)-111-1235','[email protected]'),
      (2,'David Wright','(408)-222-1234','[email protected]');

The following statement deletes the customer id 1 from the customers table:

DELETE FROM customers
WHERE customer_id = 1;

Because of the ON DELETE NO ACTION, PostgreSQL issues a constraint violation because the referencing rows of the customer id 1 still exist in the contacts table:

     ERROR:  update or delete on table "customers" violates foreign key constraint "fk_customer" on table "contacts"
DETAIL:  Key (customer_id)=(1) is still referenced from table "contacts".
SQL state: 23503

Let's walk through on terminal:

PostgreSQL Icon

And let's check on our contacts table:

\d contacts

PostgreSQL Icon

We can see that foreign-key constraints "fk_customer" were assigned successfully.

And now let's check the table data:

SELECT * FROM contacts;

SELECT * FROM customers;

PostgreSQL Icon

PostgreSQL Icon

Inner Join

INNER JOIN takes whats the common between two tables.

PostgreSQL Icon

Inner Join in PostgreSQL:

To see INNER JOIN between two tables contacts and customers type:

SELECT * FROM contacts
JOIN customers ON contacts.customer_id = customers.customer_id;

PostgreSQL Icon

Note: A JOIN simply links two tables where the primary key and foreign key is found in both tables.

Left Join

A Left Join between two tables A and B includes all rows of table A and as always the records from table B that has a corresponding relationship with table A.

PostgreSQL Icon

In case the Left Join includes those rows also where the foreign key wasn't assigned.

To show left join between contacts and customer table type:

SELECT * FROM contacts LEFT JOIN customers ON customers.customer_id = contacts.customer_id;

PostgreSQL Icon

Deleting Records with foreign keys

When we try to delete a record from customers table it will raise an error. Let's say we want to delete a record from customers table where customer_id = 2 and the error will raise something following like this:

PostgreSQL Icon

In that case, it means that first, we have to delete the record from the contacts table where the foreign key was assigned.

Let's delete the record from contacts table:

PostgreSQL Icon

This is showing the record is deleted successfully.

And then now we can delete the record from the customers table too!

PostgreSQL Icon

Exporting Query Results To CSV

We can see the directory where our files are saving by typing:

\!pwd

PostgreSQL Icon

Now let's export the csv file result.csv from a table to that location:

\copy ( SELECT * FROM contacts LEFT JOIN customers ON customers.customer_id = contacts.customer_id) TO '/var/lib/postgresql/results.csv' DELIMITER ',' CSV HEADER;

Here is the location where result.csv is exported.

PostgreSQL Icon

And here is the exported CSV file.

PostgreSQL Icon

UUID Data Type

This generates a universally unique identifier, which is a 128-bit number used to identify unique information. We can use UUID to set our primary key where we need unique sets of values.

To install or create UUID to our Postgres database we simply type:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

This is an extension or a function and to check all the other extensions just type:

select * from pg_available_extensions;

To see the installed functions:

\df

There will be several versions of UUID available. If we type the command uuid_generate_v4 every time this will generate a unique key always.

To genrate the UUID key type:

SELECT uuid_generate_v4();

PostgreSQL Icon

About

most of this is copied and pasted from other people's repositories

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published