Learn PostgreSQL - Full Course for Beginners

Learn how to use PostgreSQL in this full course. PostgreSQL is a general purpose and object-relational database management system. It is the most advanced open source database system widely used to build back-end systems.


⭐️ Contents ⭐️

⌨️ (0:03:16) What is a Database

⌨️ (0:05:17) What is SQL And Relational Database

⌨️ (0:09:10) What is PostreSQL AKA Postrgres

⌨️ (0:10:53) PostgreSQL Installation (Mac OS)

⌨️ (0:14:21) PostgreSQL Installation (Windows)

⌨️ (0:17:38) GUI Clients vs Terminal/CMD Clients

⌨️ (0:21:39) Setup PSQL (MAC OS)

⌨️ (0:25:22) Setup PSQL (Windows)

⌨️ (0:30:15) How to Create Database

⌨️ (0:33:35) How to Connect to Databases

⌨️ (0:38:12) A Very Dangerous Command

⌨️ (0:41:37) How To Create Tables

⌨️ (0:45:46) Creating Tables Without Constraints

⌨️ (0:49:12) Creating Tables with Constraints

⌨️ (0:55:55) Insert Into

⌨️ (0:59:14) Insert Into Example

⌨️ (1:02:36) Generate 1000 Rows with Mockaroo

⌨️ (1:12:28) Select From

⌨️ (1:15:18) Order By

⌨️ (1:19:53) Distinct

⌨️ (1:21:60) Where Clause and AND

⌨️ (1:25:29) Comparison Operators

⌨️ (1:29:35) Limit, Offset & Fetch

⌨️ (1:32:43) IN

⌨️ (1:35:43) Between

⌨️ (1:37:45) Like And iLike

⌨️ (1:43:10) Group By

⌨️ (1:46:41) Group By Having

⌨️ (1:52:08) Adding New Table And Data Using Mockaroo

⌨️ (1:55:40) Calculating Min, Max & Average

⌨️ (1:59:48) Sum

⌨️ (2:01:55) Basics of Arithmetic Operators

⌨️ (2:05:59) Arithmetic Operators (ROUND)

⌨️ (2:09:43) Alias

⌨️ (2:12:32) Coalesce

⌨️ (2:16:15) NULLIF

⌨️ (2:20:21) Timestamps And Dates Course

⌨️ (2:23:21) Adding And Subtracting With Dates

⌨️ (2:25:58) Extracting Fields From Timestamp

⌨️ (2:27:28) Age Function

⌨️ (2:29:24) What Are Primary Keys

⌨️ (2:31:23) Understanding Primary Keys

⌨️ (2:36:26) Adding Primary Key

⌨️ (2:40:55) Unique Constraints

⌨️ (2:49:15) Check Constraints

⌨️ (2:54:45) How to Delete Records

⌨️ (3:01:36) How to Update Records

⌨️ (3:05:55) On Conflict Do Nothing

⌨️ (3:11:09) Upsert

⌨️ (3:16:41) What Is A Relationship/Foreign Keys

⌨️ (3:19:48) Adding Relationship Between Tables

⌨️ (3:25:04) Updating Foreign Keys Columns

⌨️ (3:29:30) Inner Joins

⌨️ (3:35:17) Left Joins

⌨️ (3:40:53) Deleting Records With Foreign Keys

⌨️ (3:47:27) Exporting Query Results to CSV

⌨️ (3:50:42) Serial & Sequences

⌨️ (3:57:18) Extensions

⌨️ (3:59:39) Understanding UUID Data Type

⌨️ (4:05:54) UUID As Primary Keys

⌨️ (4:16:30) Conclusion

Thanks for reading ❤

Follow me on Facebook | Twitter


Learn More

The Complete Python & PostgreSQL Developer Course

SQL & PostgreSQL for Beginners

An Introduction to Queries in PostgreSQL

Spring Boot with PostgreSQL, Flyway, and JSONB

Node.js - Express Persistent Session Store with PostgreSQL + Sequelize

Secure Node.js, Express.js and PostgreSQL API using Passport.js

Build a Basic App with Spring Boot and JPA using PostgreSQL

Originally published at https://www.youtube.com/watch?v=qw--VYLpxG4

Learn Database Administration - PostgreSQL Database Administration (DBA) for Beginners

In this video, we will go over the basics of the PostgreSQL. We will cover topics ranging from installations, to writing basic queries and retrieving data from tables. We will also explore the logic of joining tables to retrieve data and much more.

The course also covers the basics of creating tables, storing data with data types, and working with expressions, operators, and strings.

Topics also includes:

Installing PostgreSQL

Loading sample database

Creating database and tables

Performing CRUD operations

Joining Tables

Using aggregate and analytic functions

Creating views and triggers

What you’ll learn

Install PostgreSQL Server

Load sample database

Create a database

Create a table

Insert data into tables

Update existing records inside a table

Delete Records in a table

Remove duplicate records

Query data from a table

Create a subquery

Get data from multiple tables

Create and manage roles

Create a view

Create tablespace

Backup and restore database

Filter and sort data

Use various operators

Use aggregate and analytic functions

Create triggers

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

The Complete SQL Bootcamp

The Ultimate MySQL Bootcamp: Go from SQL Beginner to Expert

An Introduction to Queries in PostgreSQL

Build a Basic App with Spring Boot and JPA using PostgreSQL

Why We Moved From NoSQL MongoDB to PostgreSQL?

What is CRUD? | CRUD Operations with SQL and PostgreSQL

What is CRUD? | CRUD Operations with SQL and PostgreSQL

What is CRUD? | CRUD Operations with SQL and PostgreSQL - In this course we will be using SQL and PostgreSQL to perform CRUD operations

In this course we will be using SQL and PostgreSQL to perform CRUD operations

PostgreSQL is a very popular , advanced, open-source object-relational database management system used by a lot of organizations. It is a very robust database management system.

Any software or a web application will typically do these set of operations called C.R.U.D.

CRUD Stands for:

  • Create (Insert)
  • Read (Select)
  • Update
  • Delete

What we will learn include:

  • How to install PostgreSQL Database Server
  • How to Load a sample database into PostgreSQL Server
  • How to Create a database and table
  • How to insert data into a table
  • How to query and retrieve data from a table
  • How to update existing data inside a table
  • How to delete data from a table
  • How to sort retrieved data from a table
  • How to Filter data using WHERE clause
  • How to remove duplicate data
  • How to use subqueries to query and retrieve data
  • How to group data using GROUP BY clause
  • How to use the HAVING clause to group data

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about CRUD, SQL and PostgreSQL

How to build a CRUD Web App with Angular 8.0

Laravel 5.8 CRUD Tutorial With Example Step By Step

MEAN Stack Angular 8 CRUD Web Application

Build a Basic CRUD App with Node and React

Learn Database Administration - PostgreSQL Database Administration (DBA) for Beginners

How to write SQL queries in PostgreSQL

How to write SQL queries in PostgreSQL

How to write SQL queries in PostgreSQL

In this tutorial, you will learn how to write simple SQL queries in PostgreSQL.

Being able to query the relational database systems is a must-have skill for a data scientist. SQL or Structured Query Language lets you do this in a very efficient way. SQL not only enables you to you ask meaningful questions to the data but also allows you to you play with the data in many different ways. Without databases, practically no real-world application is possible. So, the knowledge of databases and being able to handle them are crucial parts of a data scientist's toolbox.

Quick fact: SQL is also called SE-QU-EL. It has got some historical significance - the initial name of SQL was Simple English Query Language.

Generally, relational databases look like the following -

Relations are also called tables. There are a number of ways in which databases can be represented. This is just one of them and the most popular one.

This tutorial introduces the four most common operations performed with SQL, and they are Create, Read, Update and Delete. Collectively these four operations are often referred to as CRUD. In any application that involves user interaction when these four operations are always there.

You will be using PostgreSQL as the relational database management system. PostgreSQL is very light-weight, and it is free as well. In this tutorial, you will

  • Get up and running with PostgreSQL
  • Connect to a PostgreSQL database
  • Create, read, update and delete tables in that database
  • Run SQL on Jupyter Notebook
  • Run SQL in Python

Let's get started.

Getting up and running with PostgreSQL

PostgreSQL is a light-weight and an open source RDBMS. It is extremely well accepted by the industry. You can learn more about PostgreSQL from its official website.

To be able to start writing and executing queries in PostgreSQL, you will need it installed on your machine. Installing it is extremely easy. The following two short videos show you how PostgreSQL can be downloaded and installed on a 32-bit Windows-7 machine

Note: While you are installing PostgreSQL take note of the password and port number that you are entering.

Once you have installed PostgreSQL successfully on your machine, open up pgAdmin. pgAdmin is a handy utility which comes with the PostgreSQL installation, and it lets you do regular database related tasks through a nice graphical interface. pgAdmin's interface looks like

When you open up pgAdmin, you will see a server named "PostgreSQL 9.4 (localhost:5432)" enlisted in the interface

Note: Your version may be different than the above and so the port number (5432).

Connect to the server by entering the password that you gave during the installation. For reference - https://bit.ly/2FPO4hR.

Once you have successfully connected to the local database server, you will get an interface similar to the following

CRUD operations in PostgreSQL

Creating a table according to a given specification -

To be able to operate on a database you will need a table. So let's go ahead and create a simple table (also called relation) called datacamp_courses with the following specification (schema)

The specification gives us quite a few information on the columns of the table

  • The primary key of the table should be course_id (note that only this one is bold) and its data-type should be an integer. A primary key is a constraint which enforces the column values to be non-null and unique. It lets you uniquely identify a specific or a set of instanced present in the table.
  • Rest of the information in the specification should be easy to interpret now.

To create a table, right-click on the newly created database DataCamp_Courses and select CREATE Script from the options. You should get something similar to the following

Let's execute the following query now

CREATE TABLE datacamp_courses(
 course_id SERIAL PRIMARY KEY,
 course_name VARCHAR (50) UNIQUE NOT NULL,
 course_instructor VARCHAR (100) NOT NULL,
 topic VARCHAR (20) NOT NULL
);

For executing the query just select it and click the execute button from the menu bar

The output should be

The general structure of a table creation query in PostgreSQL looks like

CREATE TABLE table_name (
 column_name TYPE column_constraint,
 table_constraint table_constraint
)

We did not specify any table_constraints while creating the table. That can be avoided for now. Everything else is quite readable except for the keyword SERIAL. Serial in PostgreSQL lets you create an auto-increment column. By default, it creates values of type integer. Serial frees us from the burden of remembering the last inserted/updated primary key of a table, and it is a good practice to use auto-increments for primary keys. You can learn more about serial from here.

Inserting some records to the newly created table

In this step, you will insert some records to the table. Your records should contain

  • A course name
  • Instructor's name of the course
  • Course topic

The values for the column course_id will be handled by PostgreSQL itself. The general structure of an insert query in PostgreSQL looks like

INSERT INTO table(column1, column2, …)
VALUES
 (value1, value2, …);

Let's insert some records

INSERT INTO datacamp_courses(course_name, course_instructor, topic)
VALUES('Deep Learning in Python','Dan Becker','Python');

INSERT INTO datacamp_courses(course_name, course_instructor, topic)
VALUES('Joining Data in PostgreSQL','Chester Ismay','SQL');

Note that you did not specify the primary keys explicitly. You will see its effects in a moment.

When you execute the above two queries, you should get the following result upon successful insertions

Query returned successfully: one row affected, 11 ms execution time.

Reading/viewing the data from the table -

This is probably something you will do a lot in your data science journey. For now, let's see how is the table datacamp_courses holding up.

This is generally called a select query, and the generic structure of a select query looks like

SELECT
 column_1,
 column_2,
 ...
FROM
 table_name;

Let's select all the columns from the table datacamp_courses

SELECT * FROM datacamp_courses;

And you get

Note the primary keys now. If you want to just see the names of the courses you can do so by

SELECT course_name from datacamp_courses;

And you get

You can specify as many column names as possible which you may want to see in your results provided they exist in the table. If you run select course_name, number_particpants from datacamp_courses; you will run into error as the column number_particpants does exist in the table. You will now see how you can update a specific record in the table.

Updating a record in the table

The general structure of an update query in SQL looks like the following:

UPDATE table
SET column1 = value1,
    column2 = value2 ,...
WHERE
 condition;

You are going to update the record where course_instructor = "Chester Ismay" and set the course_name to "Joining Data in SQL". You will then verify if the record is updated. The query for doing this would be

UPDATE datacamp_courses SET course_name = 'Joining Data in SQL'
WHERE course_instructor = 'Chester Ismay';

Let's see if your update query had the intended effect by running a select query

You can see your update query performed exactly in the way you wanted. You will now see how you can delete a record from the table.

Deleting a record in the table

The general structure of a delete query in SQL looks like following:

DELETE FROM table
WHERE condition;

You are going to delete the record where course_name = "Deep Learning in Python" and then verify if the record is deleted. Following the structure, you can see that the following query should be able to do this

DELETE from datacamp_courses
WHERE course_name = 'Deep Learning in Python';

Keep in mind that the keywords are not case-sensitive in SQL, but the data is case-sensitive. This is why you see a mixture of upper case and lower case in the queries.

Let's see if the intended record was deleted from the table or not

And yes, it indeed deleted the intended record.

The generic structures of the queries as mentioned in the tutorial are referred from postgresqltutorial.com.

You now know how to basic CRUD queries in SQL. Some of you may use Jupyter Notebooks heavily and may be thinking it would be great if there were an option to execute these queries directly from Jupyter Notebook. In the next section, you will see how you can achieve this.

SQL + Jupyter Notebooks

To be able to run SQL queries from Jupyter Notebooks the first step will be to install the ipython-sql package.

If it is not installed, install it using:

pip install ipython-sql

Once this is done, load the sql extension in your Jupyter Notebook by executing

%load_ext sql

The next step will be to connect to a PostgreSQL database. You will connect to the database that you created -DataCamp_Courses.

For being able to connect to a database that is already created in your system, you will have to instruct Python to detect its dialect. In simpler terms, you will have to tell Python that it is a PostgreSQL database. For that, you will need psycopg2 which can be installed using:

pip install psycopg2

Once you installed psycopg connect to the database using

%sql postgresql://postgres:[email protected]:5432/DataCamp_Courses
'Connected: [email protected]_Courses'

Note the usage of %sql. This is a magic command. It lets you execute SQL statements from Jupyter Notebook. What follows %sql is called a database connection URL where you specify

  • Dialect (postgres)
  • Username (postgres)
  • Password (postgres)
  • Server address (localhost)
  • Port number (5432)
  • Database name (DaaCamp_Courses)

You can now perform everything from you Jupyter Notebook that you performed in the pgAdmin interface. Let's start by creating the table datacamp_courses with the exact same schema.

But before doing that you will have to drop the table as SQL won't let you store two tables with the same name. You can drop a table by

%sql DROP table datacamp_courses;
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
Done.





[]

The table datacamp_courses is now deleted from PostgreSQL and hence you can create a new table with this name.

%%sql
CREATE TABLE datacamp_courses(
 course_id SERIAL PRIMARY KEY,
 course_name VARCHAR (50) UNIQUE NOT NULL,
 course_instructor VARCHAR (100) NOT NULL,
 topic VARCHAR (20) NOT NULL
);
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
Done.





[]

Note the usage of %sql %%sql here. For executing a single line of query, you can use %sql, but if you want to execute multiple queries in one go, you will have to use %%sql.

Let's insert some records

%%sql
INSERT INTO datacamp_courses(course_name, course_instructor, topic)
VALUES('Deep Learning in Python','Dan Becker','Python');
INSERT INTO datacamp_courses(course_name, course_instructor, topic)
VALUES('Joining Data in PostgreSQL','Chester Ismay','SQL');
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
1 rows affected.
1 rows affected.





[]

View the table to make sure the insertions were done as expected

%%sql
select * from datacamp_courses;
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
2 rows affected.

Let's maintain the flow. As the next step, you will update a record in the table

%sql update datacamp_courses set course_name = 'Joining Data in SQL' where course_instructor = 'Chester Ismay';
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
1 rows affected.





[]

Pay close attention when you are dealing with strings in SQL. Unlike traditional programming languages, the strings values need to be wrapped using single quotes.

Let's now verify if your update query had the intended effect

%%sql
select * from datacamp_courses;
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
2 rows affected.

Let's now delete a record and verify

%%sql
delete from datacamp_courses where course_name = 'Deep Learning in Python';
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
1 rows affected.





[]
%%sql
select * from datacamp_courses;
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
1 rows affected.

By now you have a clear idea of executing CRUD operations in PostgreSQL and how you can perform them via Jupyter Notebook. If you are familiar with Python and if you interested in accessing your database through your Python code you can also do it. The next section is all about that.

Getting started with SQLAlchemy and combining it with SQL magic commands

For this section, you will need the SQLAlchemy package. It comes with the Anaconda distribution generally. You can also pip-install it. Once you have it installed, you can import it by -

import sqlalchemy

To able to interact with your databases using SQLAlchemy you will need to create an engine for the respective RDBMS where your databases are stored. In your case, it is PostgreSQL. SQLAlchemy lets you create an engine of the RDBMS in just a single call of create_engine(), and the method takes a database connection URL which you have seen before.

from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:[email protected]:5432/DataCamp_Courses')
print(engine.table_names()) # Lets you see the names of the tables present in the database
['datacamp_courses']

You can see the table named datacamp_courses which further confirms that you were successful in creating the engine. Let's execute a simple select query to see the records of the table datacamp_courses and store it in a pandas DataFrame object.

You will use the read_sql() method (provided by pandas) which takes a SQL query string and an engine.

import pandas as pd

df = pd.read_sql('select * from datacamp_courses', engine)
df.head()

You can also pair up the %sql magic command within your Python code.

df_new = %sql select * from datacamp_courses
df_new.DataFrame().head()
 * postgresql://postgres:***@localhost:5432/DataCamp_Courses
1 rows affected.