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.
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.
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.
The above command lists all the databases or newly created databases on the PostgreSQL server.
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.
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.
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”.
Give your database a name such as “book_store” in the dialog box that displays as shown in the below picture.
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.
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:
- How to Download and Install PostgreSQL on Windows
- How to Connect PostgreSQL Databases on Linux
- How to create a table in PostgreSQL [Terminal + pgAdmin]
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.