0
0
PostgreSQLquery~10 mins

Creating databases and connecting in PostgreSQL - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating databases and connecting
Start psql shell
Run CREATE DATABASE command
Database created
Connect to new database with \c
Connected to new database
Ready to run queries
This flow shows how to create a new database and then connect to it using PostgreSQL commands.
Execution Sample
PostgreSQL
CREATE DATABASE mydb;
\c mydb
Creates a new database named 'mydb' and then connects to it.
Execution Table
StepCommandActionResultNotes
1CREATE DATABASE mydb;Create new database named mydbDatabase 'mydb' createdSuccess message shown
2\c mydbConnect to database mydbYou are now connected to database "mydb".Prompt changes to mydb=#
3N/AReady to run queriesPrompt readyUser can now run queries on mydb
💡 Database created and connection established; ready for queries.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Current Databasepostgres (default)postgres (still)mydbmydb
Key Moments - 2 Insights
Why do we need to connect to the new database after creating it?
Creating a database only makes it available. To run queries inside it, you must connect to it, as shown in step 2 of the execution_table.
What happens if you try to create a database with a name that already exists?
PostgreSQL will return an error and not create a new database. This is not shown here but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current database after step 2?
Apostgres
Bmydb
Ctemplate1
Ddefault
💡 Hint
Check the 'Current Database' row in variable_tracker after step 2.
At which step does the prompt change to show the new database name?
AStep 2
BStep 3
CStep 1
DNever
💡 Hint
Look at the 'Result' and 'Notes' columns in execution_table for step 2.
If you skip the \c command after creating the database, what happens?
AThe database is deleted
BYou are connected to the new database automatically
CYou remain connected to the old database
DYou get an error
💡 Hint
Refer to the 'Current Database' variable in variable_tracker and the explanation in key_moments.
Concept Snapshot
CREATE DATABASE dbname;  -- creates a new database named dbname
\c dbname               -- connects to the database named dbname
You must connect to run queries inside the new database.
If the database exists, creation fails.
Prompt changes to show current database after connection.
Full Transcript
This visual execution shows how to create a new PostgreSQL database and connect to it. First, the CREATE DATABASE command makes a new database available. Then, the \c command connects you to that database. The prompt changes to show the new database name, indicating you are connected and ready to run queries. Variables track the current database name before and after each step. Key moments clarify why connection is needed after creation and what happens if you skip it. The quiz tests understanding of the connection step and prompt change.