We create databases to store and organize data. Connecting lets us use and manage that data.
Creating databases and connecting in PostgreSQL
Introduction
Syntax
PostgreSQL
CREATE DATABASE database_name;
\c database_nameCREATE DATABASE makes a new database with the name you choose.
\c database_name connects you to that database in the PostgreSQL command line.
Examples
myshop.PostgreSQL
CREATE DATABASE myshop;myshop database so you can start using it.PostgreSQL
\c myshop
testdb for testing purposes.PostgreSQL
CREATE DATABASE testdb;testdb database to run commands there.PostgreSQL
\c testdb
Sample Program
This creates a database called sampledb and then connects to it.
PostgreSQL
CREATE DATABASE sampledb;
\c sampledbImportant Notes
You must have permission to create databases in PostgreSQL.
Use \l to list all databases in the PostgreSQL command line.
To connect from outside the command line, use connection strings or tools like pgAdmin.
Summary
Creating a database sets up a place to store your data.
Connecting to a database lets you work with its data.
Use CREATE DATABASE and \c commands in PostgreSQL.
Practice
1. What does the
CREATE DATABASE command do in PostgreSQL?easy
Solution
Step 1: Understand the purpose of
This command is used to create a new database in PostgreSQL where data can be stored.CREATE DATABASEStep 2: Differentiate from other commands
Connecting to a database uses\c, deleting usesDROP DATABASE, and listing uses\l.Final Answer:
It creates a new database to store data. -> Option CQuick 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
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 AQuick 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?
CREATE DATABASE testdb; \c testdb SELECT current_database();
medium
Solution
Step 1: Understand command sequence
The first command creates a new database named testdb. The second command connects to it. The third command queries the current database name.Step 2: Predict output of current_database()
Since connection switched to testdb,current_database()returns 'testdb'.Final Answer:
It connects to testdb and shows 'testdb' as current database -> Option BQuick Check:
CREATE + \c + current_database() = 'testdb' [OK]
Hint: After CREATE DATABASE, use \c to switch and check with current_database() [OK]
Common Mistakes:
- Thinking \c cannot be used after CREATE DATABASE
- Assuming current_database() shows default database
- Believing connection does not change automatically
4. You run the command
CREATE DATABASE mydb but get an error. Which of these is the most likely cause?medium
Solution
Step 1: Check command syntax
The commandCREATE DATABASE mydbis valid and semicolon is optional in some clients but recommended.Step 2: Identify common permission issues
Most errors creating databases happen because the user lacks the CREATE DATABASE privilege.Final Answer:
You do not have permission to create a database. -> Option AQuick Check:
Permission error = CREATE DATABASE fails [OK]
Hint: Check user permissions if CREATE DATABASE fails [OK]
Common Mistakes:
- Assuming missing semicolon always causes error
- Thinking you cannot create database while connected to it
- Believing CREATE DATABASE is invalid in PostgreSQL
5. You want to create a new database named
salesdb and immediately connect to it using psql commands. Which sequence is correct?hard
Solution
Step 1: Understand command order
You must first create the database, then connect to it. SoCREATE DATABASE salesdb;comes before\c salesdb.Step 2: Verify command syntax
CREATE DATABASE salesdb; \c salesdb uses correct SQL and psql meta-command syntax. \c salesdb; CREATE DATABASE salesdb; tries to connect before creation. CREATE DATABASE salesdb \c salesdb; combines commands incorrectly. CREATE DATABASE salesdb; CONNECT salesdb; uses invalid CONNECT command.Final Answer:
CREATE DATABASE salesdb; \c salesdb -> Option DQuick Check:
Create then connect = correct sequence [OK]
Hint: Create first, then connect with \c [OK]
Common Mistakes:
- Trying to connect before database exists
- Using invalid commands like CONNECT
- Combining commands on one line incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
