PostgreSQL Tutorial: Complete Beginners Guide

PostgreSQL is an object-relational database management system (ORDBMS).

PostgreSQL was developed in 1986 by Michael Stonebreaker and his team.

It is easy to set up and install and offers support for both SQL and NoSQL
PostgreSQL has found its place in the market because of some of its prominent features such as its compatibility with various platforms and middlewares including Django and NodeJS.

It supports JSON and allows linking with other data stores like NoSQL which act as a federated hub for polyglot databases.


Key Commands -

Let’s now discuss some basic commands used in PostgreSQL.


  1. To create a database we can use the command - 

CREATE DATABASE database_name 

For Example - CREATE DATABASE blog

This will create a database of name “blog”

We can view all databases available by using the command-  \l

To use any database use command-  \c database_name


2. To Drop an existing database we can use -

DROP DATABASE database_name

For Example - DROP DATABASE blog

If we try to drop a table that does not exist the above command will throw an error. We can thus use an optional parameter to check whether the database exists or not by -

DROP DATABASE IF EXISTS database_name


3. Next, let‘s look at how to create a user. This can be achieved by-

CREATE USER user_name

For Example - CREATE USER Ayush

This command has many options associated with it. Some of the commonly used are SUPERUSER, CREATEDB, PASSWORD.

These can be used as - CREATE USER user_name WITH options


4. We can revoke and assign privileges to a user by following commands- 

ALTER USER user_name WITH NOSUPERUSER will revoke SUPERUSER permission from user_name

ALTER USER user_name WITH SUPERUSER will grant SUPERUSER permission to user_name


5. We can delete a user by using-

DROP USER user_name

Before deleting a user make sure that the user is an owner of the database. Otherwise, an error will occur.


6. Let’s no move to the query of creating a table. 

CREATE TABLE table_name (

field_name data_type constrain_name, 

field_name data_type constrain_name

)

For example - CREATE TABLE blog (

blog_id integer PRIMARY KEY, 

Blog_topic text 

)

To view the list of tables in a database we can use - \d


Looking for Nodejs Developers? Hire Senior Nodejs Developers - CronJ

7. Dropping a table can be done by- 

DROP TBALE table_name

For Example - DROP TABLE blog


8. Now, let‘s take a look at how to insert data in a table.

INSERT INTO Table_name (column_name1, column_name2,...column_nameN)  VALUES (value1, value2, value3,...valueN)

For Example - 

INSERT INTO blog (blog_id , blog_title)  VALUES (1 , ‘something’)

This will create a new entry in table named blog with the mentioned content.


9. Let’s now look at how can data be selected from any table

SELECT column_names FROM table_name WHERE <condition>

For Example- 

SELECT blog_id FROM blog (This will display only the column blog_id from the table)

SELECT * FROM blog (This will select all columns from the table)

SELECT blog_title FROM blog WHERE blog_id = 1 (This will select blog_title of the row whose blog_id is equal to 1)


10. Update Query in PostgreSQL can be written as- 

UPDATE table_name SET column_name1 = value1, column_name2 = value2...., column_nameN = valueN WHERE <condition>

For example - 

UPDATE blog SET blog_title = ‘PostgreSQL’ where blog_id = 1

This will update the post_title to PostgreSQL in the rows where the condition is met.



Comparison with MySQL -

Till now, we pretty much know what PostgreSQL is and how it works. So now let‘s compare PostgreSQL with MySQL.


The Biggest difference between MySQL and PostgreSQL is that the latter is free Open Source whereas the former’s source code is available under terms of GNU General Public License.

Another point of difference between the two is that MySQL is partially SQL compliant whereas PostgreSQL is largely SQL compliant. For Example - MySQL does not support check constraint.

MySQL is mostly used for web-based applications where straightforward data transactions are required whereas PostgreSQL is used where read and write speeds are important.

MySQL supports JSON data type but not any other NoSQL feature whereas PostgreSQL supports JSON as well as other NoSQL features like XML support.

MySQL supports both materialized views and temporary tables whereas PostgreSQL only supports temporary tables.

PostgreSQL has better join capabilities between tables and objects statistics as compared to MySQL.


Hire Reactjs Developers - CronJ

Application Areas of PostgreSQL -

1. Financial Industry

PostgreSQL is a very suitable database system for the financial industry. It also provides full ACID functionalities which makes it an ideal choice for OLTP (Online Transaction Processing). PostgreSQL is also capable of performing database analytics. It can be integrated with mathematical software like Matlab and R.


2. Government GIS data

GIS stands for Geographic Information System. PostgreSQL provides a very powerful GIS which is called "PostGIS". PostGIS provides a huge number of functions to process geometric data in various different formats. PostGIS is highly standard compliant.


3. Manufacturing

Nowadays, industrial manufacturers also use PostgreSQL to speed up their overall business process. It also helps them to optimize supply chain management by using  PostgreSQL as a backend.


4. Web technology and NoSQL

PostgreSQL can easily be integrated with almost all modern web frameworks like Django, Node.js, Hibernate, PHP, etc. It also offers replication capabilities that allow you to scale out as many database servers as you want.


5. Scientific data

In any research or scientific project, terabytes of data are generated. Therefore, it is important to handle in the most efficient way as possible. For that, PostgreSQL offers wonderful analytical capabilities and a powerful SQL engine. This helps to manage a large amount of data with ease.


Conclusion -

So in this blog, we studied about PostgreSQL and some of the basic commands used in it. We also discussed the difference between MySQL and PostgreSQL. In that regard, it was clearly visible that MySQL has done a great job of improving itself to be relevant, but PostgreSQL offers table inheritance, rules systems, custom data types, and database events. So, it certainly edges above MySQL.

We finished the blog with some application areas of PostgreSQL.