Back to Blog
Lesson 3 of the PostgreSQL: SQL & PostgreSQL from Scratch course
DatabasesJuly 14, 20263 min read

Creating Your Store Database: A Practical Guide

Learn database creation, connection management, and how to list databases via terminal. Start your store project with this essential PostgreSQL setup guide.

PostgreSQLSQLDatabase CreationPostgreSQL setuppsql

Previously in this course, we covered the Introduction to the Relational Model: Why PostgreSQL Wins and ensured your machine was ready in Setting Up Your PostgreSQL Environment: A Beginner's Guide. Now that your environment is configured, we need a dedicated "home" for our store's data.

In this lesson, we will move from the general server level into our specific project environment. You'll learn the fundamental commands for database creation, how to switch between different database contexts, and how to verify your progress using the terminal.

Understanding Database Scoping

In PostgreSQL, a "server" (or cluster) can host many individual databases. Think of the server as a warehouse and each database as a distinct filing cabinet inside that warehouse. Each cabinet is isolated; data in one database is invisible to another.

For our store application, we don't want to mix our tables with default system databases like postgres. We need a clean, isolated space.

Database Creation via SQL

To create a new database, we use the CREATE DATABASE command. This is a DDL (Data Definition Language) statement.

Open your terminal and connect to your default PostgreSQL instance (usually via psql -U postgres):

SQL
-- Create the database for our store project
CREATE DATABASE store_db;

Once you execute this, PostgreSQL allocates the necessary files on your disk. You’ve successfully initialized the foundation for our store application.

Connecting to Your Database

Now that store_db exists, you need to "enter" it. You cannot run table-creation commands (which we'll tackle in the next lesson) while connected to the default postgres database.

In the psql terminal, use the \c (connect) command:

Bash
\c store_db

Your terminal prompt should change from postgres=# to store_db=#. This confirms you are now operating within the scope of your new database.

Listing Databases via Terminal

It is good practice to verify your environment periodically. If you ever lose track of which databases exist on your server, use the \l (list) command:

Bash
\l

This command outputs a table showing all databases, their owners, and their encoding settings. It’s the fastest way to confirm that your store_db was created successfully and to see what else might be running on your local instance.

Quick Reference Table: Essential psql Commands

CommandAction
CREATE DATABASE name;Creates a new empty database
\c nameConnects to the specified database
\lLists all databases on the current server
\qQuits the psql terminal

Hands-on Exercise

  1. Open your terminal and connect to your local PostgreSQL server.
  2. Create a new database named my_first_store.
  3. List the databases to confirm it appears in the list.
  4. Connect to my_first_store and verify your prompt reflects the change.

Common Pitfalls

  • Forgetting the Semicolon: SQL statements must end with a semicolon (;). If you press Enter and nothing happens, you likely forgot the ; and the terminal is waiting for you to finish the command.
  • Case Sensitivity: PostgreSQL folds unquoted identifiers to lowercase. If you create a database as Store_DB, it will be stored as store_db. Stick to snake_case (all lowercase, underscores for spaces) to avoid confusion.
  • Permission Denied: If you receive a "permission denied" error, ensure you are logged in as a superuser (usually postgres) or a user with CREATEDB privileges.

Recap

You have now initialized the project database. You know how to create a database, switch between them using \c, and list them with \l. You are no longer working in the default system space; you have a dedicated environment for building your store schema.

Up next: Designing the Products Table.

Similar Posts