PostgreSQL for Beginners – Create a New Database on PostgreSQL

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:

  1. Create a new database
  2. List database
  3. 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;

or

CREATEDB 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;

Example:

How to display or list all databases 

We can easily list all the databases in the server with the following command:

\l

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. 

Syntax:

DROP DATABASE yourdatabase;

Example:

 

Thanks for reading this article. Please support us by sharing this article with the world. Cheers.

Be the first to comment

Leave a Reply

Your email address will not be published.


*