PostgreSQL Create Database

It is quite easy to create database in PostgreSQL and use them to store, process and analyze data. Here’s how to create database in PostgreSQL using PostgreSQL CREATE DATABASE command.

How to Create Database in PostgreSQL

Here are the steps to create database in PostgreSQL. We will use PostgreSQL Create Database command to create database.

Here’s the syntax for CREATE DATABASE command

CREATE DATABASE dbname;

where dbname is the name of your new database.

Here’s an example to create new database sample_db

postgres=# CREATE DATABASE sample_db;

Bonus Read : PostgreSQL CREATE TABLE

 

You can list all databases in PostgreSQL you have access to using \l (backslash el) command

postgres=# \l
                                         List of databases
   Name    |  Owner   | Encoding |      Collate       |       Ctype        |   Access privileges
-----------+----------+----------+--------------------+--------------------+-----------------------
 postgres  | postgres | UTF8     | English_India.1252 | English_India.1252 |
 sample_db | postgres | UTF8     | English_India.1252 | English_India.1252 |
 template0 | postgres | UTF8     | English_India.1252 | English_India.1252 | =c/postgres          +
           |          |          |                    |                    | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_India.1252 | English_India.1252 | =c/postgres          +
           |          |          |                    |                    | postgres=CTc/postgres

Bonus Read : How to Create User in PostgreSQL

 

You can also create database using createdb command

postgres=# createdb [option...] [dbname [description]]

In the above command,

  • dbname – name of database
  • option – command line options for createdb
  • descrition – optional description for your new database

Bonus Read : How to Create Histogram in PostgreSQL

 

How to Delete Database in PostgreSQL

You can delete database in PostgreSQL using DROP DATABASE command. Here’s the syntax of PostgreSQL DROP DATABASE command.

DROP DATABASE dbname;

where dbname is the name of database.

Here’s the SQL query to delete sample_db

postgresql=# DROP DATABASE sample_db;

 

Hopefully, now you can create database in PostgreSQL.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!