Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
PostgreSQL
2.1.
Install PostgreSQL on Windows
2.2.
Install PostgreSQL Linux
2.3.
Install PostgreSQL on macOS
3.
Set up PostgreSQL
3.1.
Create a user
3.2.
Set up a database
3.3.
Exit PostgreSQL
4.
Frequently asked questions
4.1.
What is the precondition for creating a database in PostgreSQL?
4.2.
What are the ways of creating a database in PostgreSQL?
4.3.
What is the default template in which the new database is created?
4.4.
How can we see our databases in PostgreSQL?
5.
Conclusion
Last Updated: Mar 27, 2024

Getting started with Postgresql

Author Tarun Singh
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this article, we will see the basics of Postgresql, a brief description of Postgresql, Installing Postgresql, and we will also see how to connect to the Postgresql database server. Let's get started with an introduction to Postgresql.

PostgreSQL

PostgreSQL is a powerful, open-source object-relational database management system (ORDBMS). It is used to store data securely. Some features of PostgreSQL include -

  • It runs on all major operating systems like Windows, Linux, Unix, Mac OS, etc.
  • It supports text, image, sound, and video and includes interfaces for languages like C, C++, Java, etc.
  • It supports many SQL features like complex SQL queries, foreign keys, triggers, views, transactions, concurrency, etc.
  • In PostgreSQL, tables can be set to inherit their characteristics from a "parent table."
  • We can install several extensions to add additional functionality to PostgreSQL.

Install PostgreSQL on Windows

Following are the steps to install PostgreSQL:

  1. Click the download link for PostgreSQL.

  1. When you double-click the installer file, a wizard will open that will walk you through the installation process while allowing you to select the various PostgreSQL features you want.
  2. Follow the steps as shown in the following images:
     

Step 1

 

Step 2

 

Step 3

 

Step 4

 

Step 5

 

Step 6

 

Step 7

 

Step 8

 

Step 9

We have successfully installed the software on our windows.

Install PostgreSQL Linux

PostgreSQL is integrated with the package management of most Linux distributions, including Debian, Red Hat / CentOS, SUSE, and Ubuntu.

You should install PostgreSQL in this manner since it guarantees proper operating system integration, including automated patching and other update management features.

Following are the steps to install PostgreSQL for ubuntu:

Click the download link for PostgreSQL.

Type the following command in the terminal to install the PostgreSQL in Linux:

1. To create the file repository configuration, first run the following command:

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

 

2. Import the repository signing key second:

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

 

3. update the package list

$ sudo apt-get update 

 

4. Install the latest version of PostgreSQL

$ sudo apt-get install PostgreSQL 

 

5. Use PostgreSQL-version instead of PostgreSQL if you wish to install a specific version.

$ sudo apt-get install PostgreSQL-14

Install PostgreSQL on macOS

Click the download link for PostgreSQL. When you double-click the installer file, a wizard will open that will walk you through the installation process while allowing you to select the various PostgreSQL features you want.

Follow the steps as shown in the following images:

Step 1

 

Step 2

 

Step 3

 

Step 4

 

Step 5

 

Step 6

 

Step 7

 

Step 8

 

Step 9

 

Step 10

 

Step 11

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Set up PostgreSQL

PostgreSQL is not like most other software; you can't just install it and use it immediately. You must perform two preliminary steps: PostgreSQL must first be installed and then start.

To set it up, enter:

PostgreSQL-setup –initdb 

 

This determines where the PostgreSQL databases are located on the computer. 

Now, type the following commands: 

systemctl start postgresql.service
systemctl enable postgresql.service  

 

The first command launches PostgreSQL for your computer's current session (if you turn it off, PostgreSQL shuts down). On subsequent reboots, PostgreSQL will launch thanks to the second command automatically.

Create a user

You can't use PostgreSQL even though it is running since you haven't been given a user name yet. You must switch to the Postgres special user to accomplish this. While you are still logged in as root, type the following command:

su Postgres 

 

There is no need to enter a password because you are doing this action as root. The root user is compelling and dangerous since it can act as any user without knowing their password.

Now that you're using Postgres, run two commands to create your user, like the example below (which creates the user cnUser):

createuser cnUser
createdb cnUser 

Set up a database

On the command line, type psql to launch PostgreSQL. To indicate that you are using PostgreSQL and can only use commands that it understands, you should see something like cnUser=> to the left of each line. You already have a database that contains nothing. In the context of PostgreSQL, a database is only a workspace. You build tables in that area. The data that makes up your database is listed underneath each variable in a table that has a list of variables. 

CREATE TABLE demoTable (
    date date,
    name varchar(60),
    place varchar(60),
    cost numeric(6,2)
); 

Exit PostgreSQL

Finally, enter the following in the terminal to exit PostgreSQL: 

quit  

 

Or you can type

\q 

Frequently asked questions

What is the precondition for creating a database in PostgreSQL?

To create a database in PostgreSQL, the user must be a superuser or have the appropriate privileges to create a database.

What are the ways of creating a database in PostgreSQL?

There are two ways of creating a database in PostgreSQL. These are:

  1. Using CREATE DATABASE command on PostgreSQL shell prompt.
  2. Using createdb command on command prompt.

What is the default template in which the new database is created?

The default template in which the new database is created is template1. It is an empty database that contains some default database objects.

How can we see our databases in PostgreSQL?

We can see our databases in PostgreSQL using the \l (backslash l) command. \l or \list is a psql tool that lists all the databases present in the current server. 

Conclusion

In this blog, we have extensively discussed getting started with PostgreSQL and have seen the installation process in detail for Windows, Linux, and macOS.We also learned how to use the PostgreSQL database in brief. I hope you like the content.

If you want to explore more about PostgreSQL and about other databases then refer to these links, MongoDBDatabases, and relational databasesnon-relational databases. Also, look at the Coding Ninjas website for some great information, Web DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series

Do upvote our blog to help other ninjas grow.

Happy Coding!

Previous article
PostgreSQL Drop Database
Next article
Postgresql Coalesce
Live masterclass