The CREATE DATABASE statement is used to create a new database in PostgreSQL.
Each database acts as a container that holds tables, views, functions, and other database objects.
In PostgreSQL, you can create a new database using the CREATE DATABASE SQL command in the psql shell. For those who prefer a graphical interface, pgAdmin provides an easy and user-friendly way to manage databases.
Let’s walk through the step-by-step process of creating a PostgreSQL database.
Creating a Database with Default Settings
In this example, we’ll create a basic test database using PostgreSQL’s default configuration. When you don’t specify optional parameters such as encoding or connection limits, PostgreSQL automatically applies its defaults—like UTF8 encoding, the default tablespace, and no restriction on connections.
Create a Simple Database
This creates a new database named school.
CREATE DATABASE school;
Create Database with Owner
Creates a database training and assigns ownership to the user ravi.
CREATE DATABASE training OWNER ravi;
Create the Role (User)
CREATE ROLE ravi WITH LOGIN PASSWORD 'your_password';