Hello everyone, welcome to PostgreSQL Tutorial for Beginners. Today, we are going to learn about how to create a new database on PostgreSQL. On this example, we are going to create a new database via command line. Actually, you can create a new database via PgAdmin and we will show you this later. On this tutorial, our PostgreSQL was installed on Raspberry Pi and I connect to it from an Ubuntu 18.04 machine.
What will you learn?
On this article, you will learn the following:
- Create a new database
- List database
- Drop or delete a database
All those tasks can be done using Terminal console.
Create new database syntax
Generally, we can create a new database using the following syntax.
CREATE DATABASE newdatabase;
Let’s get started. First, switch to postgres user.
sudo - postgres
Now create a new database. For example, lets call our new database as “database01”.
psql CREATE DATABASE database01;
How to display or list all databases
We can easily list all the databases in the server with the following command:
The command must be executed inside the psql. So, run psql first and then the \l command. For example:
Delete or Drop a Database
In some cases, you may need to delete or drop your database(s). See how to delete a database using psql from Terminal console.
DROP DATABASE yourdatabase;
Thanks for reading this article. Please support us by sharing this article with the world. Cheers.