Creating A Database Using PostgreSQL

Introduction

 
Creating a database in PostgreSQL is the same on Windows and all other operating systems.
 
Now that we have our database running, we need a way to connect to it.
 
Remember that our computer is serving as a computer server, i.e. a database server. This way anyone can connect to it and view the content modify the content.
 
So the way to connect to the database server:
  • GUI client
  • Terminal/CMD
  • Application

GUI client

  • The very first way to connect to the database server is a GUI client. This is an application that eases the way you connect to the database. It makes life easy in terms of performing inserts, delete, view the data, and have like all these fancy UI elements that allow you to see your database in a much easy way.
  • Let me give you a GUI client name so that you can have a brief idea about that.
  • One of the popular GUI out in the market is DataGrip(paid) just go through the site and have a brief idea about how GUI is used in the database management system.
  • postico https://eggerapps.at/postico/ (only for mac)
  • For windows, you can use pgAdmin: https://www.pgadmin.org/

Terminal/CMD

 
The second way is using the Terminal/Command line and this is my preferred choice and this is because this is how you get your hands dirty by learning all the commands that your database of choice required to manipulate your database and once you learn how to use Terminal or command line which is not difficult, to be honest than using a GUI client is very trigger.
 

Application

 
The third way is using an application. So for example, you write a server-side application where you connect your database and can perform data manipulation, and then you can return the data to your client so the client can make the data look nice on the screen or a mobile application.
 
To create a database, follow the below steps:
 
 
Now let's create a database.
 
So to create a database in PostgreSQL
 
Use the command CREATE DATABASE
 
Let's make a database "test".
 
  1. CREATE DATABASE test;   
 
Now let us check whether we have created the database with the name "test". 
 
To check that use the command 
 
\l
 
 
As you can see in the above image, we have successfully created a new database with the name "test". 
 
Before adding some tables to our new database, let's see how to connect to a particular database. 
 
So the command for connecting the database is:
 
\c test // \c database name
 
 
After connecting to the database which you want the message appears saying that "you are now connected to database "database name" as user "username" ".
 
Now that we are connected to the database we created, we should add some tables to it. 
 
To create a table, we use commands such as:
 
  1.  CREATE TABLE person(test(#id int,  
  2. test(# first_name VARCHAR(50),  
  3. test(# last_name VARCHAR(50),  
  4. test(# gender VARCHAR(6),  
  5. test(# date_of_birth DATE);  
 
After creating tables, let's see if the tables are created successfully.
 
So for checking the table, the command used is: 
 
\d
\d person
 
 
As you can see in the above image, we have successfully created a table in our new database.
 
So there you go, you created your first PostgreSQL database and also added a table to it.
 

Conclusion 

 
In this article, you learned how to create a database in PostgreSQL, how to connect to the database you made, and how to add tables to them.
 
To know more about PostgreSQL check the below link:


Similar Articles