PostgreSQL Create Database

Database is collection of objects. Objects can be tables, views, stored procedures, triggers, functions, indexes, etc. CREATE DATABASE is the command used to create database in PostgreSQL Server.

  • In this tutorial, we will Create Database from command line and pgAdmin tool.
  • To create a database, you must be a superuser or have the special CREATEDB privilege.
  • By default, the new database will be created by cloning the standard system database template1. A different template can be specified by writing TEMPLATE name.

PostgreSQL Create Database Syntax:

CREATE DATABASE name [ [ WITH ] [ OWNER [=] user_name ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] [ LC_COLLATE [=] lc_collate ] [ LC_CTYPE [=] lc_ctype ] [ TABLESPACE [=] tablespace_name ] [ ALLOW_CONNECTIONS [=] allowconn ] [ CONNECTION LIMIT [=] connlimit ] [ IS_TEMPLATE [=] istemplate ] ]

PostgreSQL Create Database from command line:

By default owner of the database is currently logged in user.

1. PostgreSQL Create Database

CREATE DATABASE ecommerce;

2. PostgreSQL Create Database with owner name

CREATE DATABASE sale owner=postgres;

3. Verify list of databases in PostgreSQL

We can get the list of PostgreSQL databases by using meta command \l or using table pg_database.

PostgreSQL Create Database

4. Switch to database in PostgreSQL

\c ecommerce

PostgreSQL Create Database from pgAdmin tool

1. Open pgAdmin tool and right click on databases, then select New Database.

2. Provide the name and click ok. Then new database appears in the databases.

So in this article, we have seen two different methods for PostgreSQL Create Database.