PostgreSQL Lesson 8 – Creating Databases & Schemas | Dataplexa

Creating Databases & Schemas

This is the lesson where everything becomes hands-on. From here onwards every concept you learn will be practiced on a real database with real data. In this lesson you will create a PostgreSQL database, understand what schemas are and why they matter, load the course dataset, and verify everything is working. By the end your environment is fully set up and ready for every lesson ahead.

The Course Dataset — Load It Now

If you have not downloaded the course dataset yet, download it now. Every lesson from this point forward uses it. The dataset is a realistic online store database called Dataplexa Store. It has five tables — customers, products, orders, order_items, and employees — with hundreds of rows of real-looking US-based data covering customers, a product catalogue with prices and categories, orders with dates and statuses, line items, and employee records.

📦 Dataplexa Store — Course Dataset

dataplexa-store-dataset.sql  ·  5 tables  ·  customers, products, orders, order_items, employees

⬇ Download Dataset

What is a Database in PostgreSQL

A database in PostgreSQL is the top-level container. It holds everything — schemas, tables, views, functions, indexes, and users. When you connect with psql or pgAdmin, you always connect to a specific database. Different databases on the same PostgreSQL server are completely isolated from each other — a query in one database cannot access tables in another database directly.

When PostgreSQL is first installed, three databases exist by default. postgres is the default maintenance database used for administrative connections. template0 and template1 are template databases — when you create a new database, PostgreSQL copies template1 as the starting point. You should never modify template0.

Creating a Database

You create a new database using the CREATE DATABASE command. Database names should be lowercase, use underscores instead of spaces, and be descriptive. Once created, connect to it immediately using \c in psql.

-- Create the course database
CREATE DATABASE dataplexa_store;

-- List all databases to confirm it was created
\l

-- Connect to the new database
\c dataplexa_store
CREATE DATABASE

   Name               | Owner    | Encoding
-----------------------+----------+---------
 dataplexa_store      | postgres | UTF8
 postgres             | postgres | UTF8
 template0            | postgres | UTF8
 template1            | postgres | UTF8

You are now connected to database "dataplexa_store" as user "postgres".

Database Options You Can Set

When creating a database you can specify additional options — the owner, encoding, and locale. For most projects you will not need to change these, but knowing them helps when working in multi-user or international environments.

-- Create a database with specific options
CREATE DATABASE dataplexa_store
  OWNER = postgres           -- who owns this database
  ENCODING = 'UTF8'          -- character encoding (UTF8 supports all languages)
  LC_COLLATE = 'en_US.UTF-8' -- sort order rules
  LC_CTYPE = 'en_US.UTF-8'   -- character classification
  TEMPLATE = template1;      -- which template to copy from

-- Rename a database
ALTER DATABASE dataplexa_store RENAME TO dataplexa_production;

-- Rename it back
ALTER DATABASE dataplexa_production RENAME TO dataplexa_store;

-- Drop (delete) a database — WARNING: this is permanent and cannot be undone
DROP DATABASE IF EXISTS old_database;
CREATE DATABASE
ALTER DATABASE
ALTER DATABASE
DROP DATABASE

What is a Schema

A schema is a namespace inside a database — a way to organise tables, views, functions, and other objects into logical groups. Think of a database as a building and schemas as floors. Each floor organises its own set of rooms (tables). Different floors can have rooms with the same name without any conflict.

Every PostgreSQL database has a default schema called public. When you create a table without specifying a schema, it goes into public automatically. This is perfectly fine for learning and small projects. In larger systems, schemas help you separate concerns — for example, a sales schema for sales tables, a hr schema for employee tables, and an analytics schema for reporting views.

Creating and Using Schemas

-- Create a new schema
CREATE SCHEMA sales;
CREATE SCHEMA hr;
CREATE SCHEMA analytics;

-- List all schemas in the current database
SELECT schema_name FROM information_schema.schemata;

-- Create a table inside a specific schema
-- Format: schema_name.table_name
CREATE TABLE sales.orders (
  id SERIAL PRIMARY KEY,
  amount NUMERIC
);

-- Query a table in a specific schema
SELECT * FROM sales.orders;

-- Set the default schema for your session
-- so you don't need to type the schema name every time
SET search_path TO sales;
CREATE SCHEMA
CREATE SCHEMA
CREATE SCHEMA

 schema_name
--------------
 public
 sales
 hr
 analytics

CREATE TABLE

The search_path — How PostgreSQL Finds Tables

When you type SELECT * FROM orders without a schema prefix, PostgreSQL looks through the search_path to find the table. By default the search_path is "$user", public — it first looks for a schema matching your username, then falls back to public. If no table named orders is found in any schema on the search_path, you get an error.

-- Check the current search_path
SHOW search_path;

-- Change search_path for the current session
SET search_path TO sales, public;

-- Change it permanently for a user
ALTER ROLE postgres SET search_path TO sales, public;
  search_path
----------------
 "$user", public

Loading the Course Dataset

Now that your dataplexa_store database is created, it is time to load the dataset. The \i command in psql runs every SQL statement inside a file. Make sure you are connected to the dataplexa_store database first, then run the command below, replacing the path with wherever you saved the file on your computer.

-- Make sure you are connected to the right database first
\c dataplexa_store

-- Load the dataset — replace the path with your actual file location
-- On Mac/Linux:
\i /Users/yourname/Downloads/dataplexa-store-dataset.sql

-- On Windows:
\i C:/Users/yourname/Downloads/dataplexa-store-dataset.sql
You are now connected to database "dataplexa_store".
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
INSERT 0 50
INSERT 0 30
INSERT 0 200
INSERT 0 350
INSERT 0 20

Verifying the Dataset Loaded Correctly

After loading, run these quick checks to confirm all five tables exist and contain data.

-- List all tables in the database
\dt

-- Check row counts for all five tables
SELECT 'customers'   AS table_name, COUNT(*) AS rows FROM customers
UNION ALL
SELECT 'products',   COUNT(*) FROM products
UNION ALL
SELECT 'orders',     COUNT(*) FROM orders
UNION ALL
SELECT 'order_items',COUNT(*) FROM order_items
UNION ALL
SELECT 'employees',  COUNT(*) FROM employees;
 table_name   | rows
--------------+------
 customers    |   50
 products     |   30
 orders       |  200
 order_items  |  350
 employees    |   20

If you see those five tables with row counts, your dataset is loaded and ready. Every lesson from here will use this data for practice.

Dropping and Managing Schemas

-- Drop a schema (only works if the schema is empty)
DROP SCHEMA analytics;

-- Drop a schema and everything inside it
DROP SCHEMA analytics CASCADE;

-- Rename a schema
ALTER SCHEMA sales RENAME TO store_sales;

-- Transfer ownership of a schema
ALTER SCHEMA hr OWNER TO another_user;
DROP SCHEMA
DROP SCHEMA
ALTER SCHEMA
ALTER SCHEMA

Lesson Summary

Concept Syntax What It Does
Create database CREATE DATABASE name; Creates a new empty database
Switch database \c dbname Connects to a different database
Drop database DROP DATABASE name; Permanently deletes a database
Create schema CREATE SCHEMA name; Creates a namespace inside a database
Default schema public Where tables go when no schema is specified
Schema-qualified table schema.tablename Accesses a table in a specific schema
Search path SET search_path TO name; Sets default schema for the session
Load SQL file \i /path/to/file.sql Runs all SQL in a file inside psql

🧪 Practice Questions

Answer based on what you learned in this lesson.

1. What is the name of the default schema in every PostgreSQL database?




2. What PostgreSQL setting controls which schemas are searched when you reference a table without a schema prefix?




3. What keyword do you add to DROP SCHEMA to also delete all tables and objects inside it?




4. Which default PostgreSQL database is used as the template when creating a new database?




5. Which psql command loads and runs all SQL statements from a file?



🎯 Quiz — Test Your Understanding

Q1. What is a schema in PostgreSQL?







Q2. What is the relationship between two different databases on the same PostgreSQL server?







Q3. Which command permanently deletes an entire PostgreSQL database?







Q4. How do you reference a table called orders that lives inside the sales schema?







Q5. What happens if you try to DROP a database while you are currently connected to it?






Next up: Every data type PostgreSQL supports — and how to choose the right one.