How to Create a Database in PostgreSQL
Learn how to create a PostgreSQL database using SQL commands or pgAdmin, verify it exists, and understand database naming rules and common errors.
1. Introduction
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:
- Student records
- Teacher records
- Department information
- Course information
- Examination results
To manage this information, we first need to create a database.
Example: We can create a database named university to manage university-related information.
2. What is CREATE DATABASE?
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.
3. Understanding the SQL Command
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 (;).
4. General Syntax
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.
5. Creating a Database Using pgAdmin
pgAdmin is a graphical user interface (GUI) tool used to manage PostgreSQL databases.
Follow these steps to create a database.
- Open pgAdmin.
- Connect to your PostgreSQL server.
- Expand the Servers section.
- Expand the PostgreSQL server.
- Right-click on Databases.
- Select Create → Database.
- Enter
universityin the Database name field. - Click Save.
The university database will appear under the Databases section.
6. Creating a Database Using SQL
We can also create a database by writing an SQL command in the Query Tool.
Step 1: Open Query Tool
- Select an existing database, such as
postgres. - Open the Query Tool.
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.
7. How to Verify a Database
After creating a database, we should check whether it exists.
We can use the following SQL query:
SELECT datname
FROM pg_database;
Explanation:
SELECTretrieves information.datnamerepresents the database name.FROM pg_databasespecifies the PostgreSQL system catalog containing database information.
The query displays the available databases on the PostgreSQL server.
8. Important Points to Remember
- A database must have a name.
- Database names must be unique within the same PostgreSQL server.
- A user needs appropriate permission to create a database.
- Use meaningful database names.
- Use lowercase names without spaces for beginners.
- A database can be created using pgAdmin or SQL commands.
- Creating a database does not automatically create tables inside it.
9. Common Errors
| 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.
10. Practice Exercises
Exercise 1: Write SQL Commands
Write SQL commands to create the following databases:
schoolcollegeuniversitystudent_dblibrary
Exercise 2: Practical Activity
Perform the following tasks using pgAdmin:
- Open pgAdmin and connect to PostgreSQL.
- Create a database named
university. - Create another database named
college. - Verify both databases in pgAdmin.
- Write an SQL query to display all database names.
- Try creating the
universitydatabase again and observe the error.
11. Review Questions
- What is a database?
- What is the purpose of the
CREATE DATABASEcommand? - Write the general syntax for creating a database.
- What is the purpose of the semicolon in SQL?
- What is pgAdmin?
- What happens if we try to create a database that already exists?
- How can we verify that a database has been created?
- Does creating a database automatically create tables?
12. Quick Revision
| 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.