📖 Scenario: You are setting up a new project that requires its own database. You will create a PostgreSQL database and then connect to it to start working with your data.
🎯 Goal: Create a new PostgreSQL database named project_db and then connect to it using the psql command-line tool.
📋 What You'll Learn
Create a database named project_db
Connect to the project_db database using psql
Use exact commands as specified
💡 Why This Matters
🌍 Real World
Creating and connecting to databases is the first step in managing data for applications, websites, and data analysis projects.
💼 Career
Database creation and connection skills are essential for database administrators, backend developers, and data engineers.
Progress0 / 4 steps
1
Create the database
Write the SQL command to create a new database called project_db.
PostgreSQL
Hint
Use the CREATE DATABASE statement followed by the database name.
2
Connect to the database
Write the psql command to connect to the database named project_db.
PostgreSQL
Hint
Use the \c command in psql to connect to a database.
3
Verify the connection
Write the SQL command to show the current connected database name using SELECT current_database();.
PostgreSQL
Hint
Use the SELECT current_database(); command to check which database you are connected to.
4
Complete the setup
Add a comment line that says -- Database setup complete to mark the end of your setup script.
PostgreSQL
Hint
Add a comment line starting with -- to indicate the setup is done.
Practice
(1/5)
1. What does the CREATE DATABASE command do in PostgreSQL?
easy
A. It deletes a database permanently.
B. It connects to an existing database.
C. It creates a new database to store data.
D. It lists all databases available.
Solution
Step 1: Understand the purpose of CREATE DATABASE
This command is used to create a new database in PostgreSQL where data can be stored.
Step 2: Differentiate from other commands
Connecting to a database uses \c, deleting uses DROP DATABASE, and listing uses \l.
Final Answer:
It creates a new database to store data. -> Option C
Quick Check:
CREATE DATABASE = create new database [OK]
Hint: Remember: CREATE DATABASE makes a new database [OK]
Common Mistakes:
Confusing CREATE DATABASE with connecting commands
Thinking it deletes or lists databases
Using wrong command for creating database
2. Which of the following is the correct syntax to connect to a database named shopdb in PostgreSQL?
easy
A. \c shopdb
B. \\c shopdb
C. CONNECT TO shopdb;
D. USE shopdb;
Solution
Step 1: Identify the correct psql command for connecting
In PostgreSQL's psql tool, the command to connect to a database is \c database_name.
Step 2: Check syntax correctness
\c shopdb uses the correct backslash and no extra backslashes or SQL keywords.
Final Answer:
\c shopdb -> Option A
Quick Check:
Connect command = \c [OK]
Hint: Use single backslash \c to connect in psql [OK]
Common Mistakes:
Using double backslashes instead of one
Using SQL commands like USE which is not PostgreSQL
Adding semicolons to psql meta-commands
3. What will be the result of running the following commands in psql?