Learn how to create a PostgreSQL database using SQL commands or pgAdmin, verify it exists, and understand database naming rules and common errors.
A database is an organized collection of information that allows us to store, manage, retrieve, and update data.
In a university, we need to store different types of information, such as:
To manage this information, we first need to create a database.
Example: We can create a database named university to manage university-related information.
CREATE DATABASE is an SQL command used to create a new database in PostgreSQL.
In simple words, it tells PostgreSQL to create a new place where we can organize and store information.
For example:
CREATE DATABASE university;
This command creates a new database named university.
Let’s understand each part of the command.
CREATE DATABASE university;
| Part | Explanation |
|---|---|
CREATE |
Tells PostgreSQL to create something new. |
DATABASE |
Specifies that we want to create a database. |
university |
Name of the database. |
; |
Indicates the end of the SQL statement. |
Remember: Every SQL statement should normally end with a semicolon (;).
The general syntax for creating a database is:
CREATE DATABASE database_name;
Here, database_name represents the name of the database we want to create.
Examples:
CREATE DATABASE university;
CREATE DATABASE college;
CREATE DATABASE student_db;
Each command creates a separate database with the specified name.
pgAdmin is a graphical user interface (GUI) tool used to manage PostgreSQL databases.
Follow these steps to create a database.
|  |
university in the Database name field.The university database will appear under the Databases section.
We can also create a database by writing an SQL command in the Query Tool.
Step 1: Open Query Tool
postgres.Step 2: Write the SQL command
CREATE DATABASE university;
Step 3: Execute the command
Click the Execute button or press F5.
Expected Result:
CREATE DATABASE
This message indicates that PostgreSQL has successfully created the database.
Important
The CREATE DATABASE command cannot be executed inside a transaction block. Run it as a standalone statement.
After creating a database, we should check whether it exists.
We can use the following SQL query:
SELECT datname
FROM pg_database;
Explanation:
SELECT retrieves information.datname represents the database name.FROM pg_database specifies the PostgreSQL system catalog containing database information.The query displays the available databases on the PostgreSQL server.
| Incorrect Command | Correct Command |
|---|---|
CREATE university; |
CREATE DATABASE university; |
CREATE DATABASE; |
CREATE DATABASE university; |
CREATE DATABASE university |
CREATE DATABASE university; |
Duplicate database error:
If a database named university already exists, PostgreSQL will not allow us to create another database with the same name.
Exercise 1: Write SQL Commands
Write SQL commands to create the following databases:
schoolcollegeuniversitystudent_dblibraryExercise 2: Practical Activity
Perform the following tasks using pgAdmin:
university.college.university database again and observe the error.CREATE DATABASE command?| Concept | Command / Explanation |
|---|---|
| Create database | CREATE DATABASE university; |
| General syntax | CREATE DATABASE database_name; |
| List databases | SELECT datname FROM pg_database; |
| GUI tool | pgAdmin |
| SQL execution | Execute button / F5 |
Key Takeaway: The CREATE DATABASE command is the first step in building a database application in PostgreSQL. Before creating tables and inserting records, we must have a database in which to organize our information.