How to Create Database in PostgreSQL

In this PostgreSQL tutorial, we will learn about How to Create a Database in PostgreSQL using two methods, using the terminal and the Graphical User Interface (GUI) called pgAdmin.

How to Create Database in PostgreSQL Using Terminal

Before beginning to create a database, make sure that you have already installed PostgreSQL on your computer. To create a new database using the terminal or CMD, first, you need to get access to the PostgreSQL command-line interface.

So use the below query to get access to the PostgreSQL command-line interface.

psql -U username;

Change the “username” to your PostgreSQL username.

“psql” is the command to start the PostgreSQL interactive terminal. It enables you to communicate directly with the PostgreSQL database server. You can use the “-U” option to enter the username you want to use to log in.

  • Remember while login, if it asks for a password then provide the password for the PostgreSQL username.
How to Create Database in PostgreSQL Using Terminal Get Access to PostgreSQL CLI
How to Create Database in PostgreSQL Using Terminal Get Access to PostgreSQL CLI

Once you have logged into or got access to the PostgreSQL terminal, you can begin to create a new database using the below command.

CREATE DATABASE database_name;

Change the “database_name” to the name you want to give your database.

To create a new database on the PostgreSQL server, specify the command “CREATE DATABASE”. It gives the instructions to the server to allocate resources and make a new database based on the given specifications.

How to Create Database in PostgreSQL Using Terminal Creating Database
How to Create Database in PostgreSQL Using Terminal Creating Database

In the above example, Created a database named “book_store” using the command “CREATE DATABASE”.

It’s time to verify or view the created database using the below command.

\l

The above command lists all the databases or newly created databases on the PostgreSQL server.

How to Create Database in PostgreSQL Using Verifying Database
How to Create Database in PostgreSQL Using Verifying Database

In the above picture, you can see the newly created database named “book_store” that is listed using the command “\l”.

How to Create Database in PostgreSQL Using pgAdmin

A graphical user interface (GUI) for managing PostgreSQL databases is provided by pgAdmin. Launch the pgAdmin program on your computer to get started.

How to Create Database in PostgreSQL Using pgAdmin Launch pgAdmin
How to Create Database in PostgreSQL Using pgAdmin Launch pgAdmin

You have to set up a connection to the PostgreSQL server after starting pgAdmin. Find the “Servers” group in the left sidebar. Then select the server on which you want to create the database, while selecting the server, it asks for a password then enter the password.

How to Create Database in PostgreSQL Using pgAdmin Connecting to PostgreSQL Server
How to Create Database in PostgreSQL Using pgAdmin Connecting to PostgreSQL Server

Expand the server’s tree structure on the left sidebar after connecting to it. Go to the “Databases” node and, using the right-click menu, choose “Create” > “Database”.

How to Create Database in PostgreSQL Using pgAdmin Creating Database
How to Create Database in PostgreSQL Using pgAdmin Creating Database

Give your database a name such as “book_store” in the dialog box that displays as shown in the below picture.

How to Create Database in PostgreSQL Using pgAdmin
How to Create Database in PostgreSQL Using pgAdmin

Expand the “Databases” node in the server tree structure to verify that the database has been created successfully. Your newly created database should appear in the list of existing databases.

How to Create Database in PostgreSQL Using pgAdmin Verifying Database Existence
How to Create Database in PostgreSQL Using pgAdmin Verifying Database Existence

Conclusion

In this PostgreSQL tutorial, we learned two ways to create a database using the terminal and the graphical user interface of pgAdmin. Both ways allow us to create databases, whether we want the GUI for its visual representation or the command-line interface for its ease of use.

You may like to read: