1. Syntax to Create a Database
CREATE DATABASE database_name;
What it does:
This command creates a new database named database_name
in your PostgreSQL server.
Where to type:
You enter this command inside the psql
command line interface after connecting to your PostgreSQL server.
2. Step-by-Step Guide
- Open Terminal/Command Prompt: Open the terminal on your computer where PostgreSQL is installed.
-
Connect to PostgreSQL Server: Type this command and press Enter:
psql -U postgres
This logs you into PostgreSQL as the user
postgres
(the default superuser). -
Create the Database: Once connected, type the syntax to create your database:
CREATE DATABASE database_name;
Replace
database_name
with your desired database name. Then press Enter. -
Expected Output:
CREATE DATABASE
This confirms the database was successfully created.
-
Verify the Database: To see the list of all databases, type:
\l
You will see a list including your new database.
-
Connect to Your Database: To start using your new database, type:
\c database_name
You will see confirmation:
You are now connected to database "database_name".
Reference: Official PostgreSQL Documentation: CREATE DATABASE