Learn about the PostgreSQL DVD Rental sample database, its tables, relationships, installation, restoration, and basic SQL queries using pgAdmin 4.
The DVD Rental database (dvdrental) is a sample database provided for learning and practising PostgreSQL.
It represents a DVD rental business where customers can rent movies from different stores.
The database contains realistic business information, including:
The dvdrental database contains 15 main tables in the public schema.
It is particularly useful for students who want to learn SQL using realistic business data instead of creating small sample tables themselves.
Before working with SQL queries, let’s understand how the DVD rental business works.
Imagine a company that operates several DVD rental stores.
The database stores information about all these activities.
Example:
A customer named John rents the movie Academy Dinosaur from a store.
The database can maintain:
film table.inventory table.customer table.rental table.payment table.This business scenario helps students understand why databases contain multiple related tables.
The database contains 15 main tables. Each table stores information about a particular part of the business.
| Table | Purpose |
|---|---|
film |
Stores movie titles, descriptions, release years, rental rates, and other movie details. |
actor |
Stores the first and last names of actors. |
film_actor |
Connects movies with their actors. |
category |
Stores movie categories such as Action, Comedy, and Horror. |
film_category |
Connects movies with their categories. |
language |
Stores the languages associated with movies. |
| Table | Purpose |
|---|---|
inventory |
Stores information about individual DVD copies available in stores. |
store |
Stores information about DVD rental stores. |
staff |
Stores employee information. |
| Table | Purpose |
|---|---|
customer |
Stores customer names, email addresses, and account information. |
rental |
Stores DVD rental transactions, including rental and return dates. |
payment |
Stores customer payment transactions. |
| Table | Purpose |
|---|---|
address |
Stores street addresses and postal information. |
city |
Stores city names. |
country |
Stores country names. |
Total: 15 main tables
Before practising SQL queries, students need to download and install the sample database.
The database is available in a compressed ZIP file containing a PostgreSQL backup file.
Download the database:
Download DVD Rental Sample Database
Alternative source:
DVD Rental PostgreSQL Project on GitHub
dvdrental.zip.dvdrental.tar backup file.The .tar file contains the database backup that we will restore in PostgreSQL.
Before restoring the backup, we need to create an empty database.
dvdrental
A new empty database named dvdrental will be created.
Now we will restore the downloaded backup file into the newly created database.
dvdrental database.A Restore dialog box will appear.
Configure the following settings:
| Setting | Value |
|---|---|
| Format | Custom or tar |
| Filename | Select dvdrental.tar |
| Role name | postgres (or an appropriate database role) |
Use the folder icon to browse and select the downloaded backup file.
If the file is not visible, change the file type filter to All Files.
Open the Restore options section.
Ensure the following backup sections are enabled:
| Option | Setting |
|---|---|
| Pre-data | Yes |
| Data | Yes |
| Post-data | Yes |
| Do not save owner | Yes (recommended when restoring under a different database user) |
dvdrental database in the pgAdmin browser tree.After successful restoration, the database tables will be available.
Sometimes pgAdmin displays an error indicating that it cannot find the pg_restore utility.
To resolve this issue:
bin directory.Example:
C:\Program Files\PostgreSQL\16\bin
Replace 16 with your installed PostgreSQL version.
After restoring the database, let’s explore its structure.
Follow these steps:
dvdrental database.public schema.You will see the available database tables.
The public schema contains the 15 main tables used in our exercises.
We can also retrieve the names of all tables using an SQL query.
Example: Display all tables in the public schema
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE'
ORDER BY table_name;
Understanding the Query:
SELECT table_name: Displays table names.FROM information_schema.tables: Retrieves information about database tables.WHERE table_schema = 'public': Selects tables from the public schema.table_type = 'BASE TABLE': Includes actual tables, not views.ORDER BY table_name: Sorts the table names alphabetically.Expected Result: A list of the 15 main tables.
Now let’s execute our first queries using the film table.
SELECT *
FROM public.film;
The * symbol means that we want to retrieve all columns.
SELECT title
FROM public.film;
This query displays only the title column.
SELECT *
FROM public.film
LIMIT 10;
The LIMIT 10 clause restricts the result to the first 10 records returned by the query.
The customer table stores information about customers registered with the DVD rental business.
SELECT *
FROM public.customer;
SELECT
first_name,
last_name,
email
FROM public.customer;
This query retrieves only the first name, last name, and email address of each customer.
SELECT *
FROM public.customer
LIMIT 10;
The DVD Rental database contains relationships between different tables.
These relationships allow us to retrieve related information from multiple tables.
Let’s understand a few examples.
One customer can rent multiple DVDs.
customer stores customer information.rental stores rental transactions.customer_id column connects these tables.Relationship: One-to-Many.
One film can have multiple physical DVD copies.
film stores movie information.inventory stores individual DVD copies.film_id column connects these tables.Relationship: One-to-Many.
A rental transaction can be associated with payment records.
rental stores rental information.payment stores payment information.rental_id column connects these tables.One film can have multiple actors, and one actor can appear in multiple films.
The film_actor table connects the film and actor tables.
Relationship: Many-to-Many.
We will study these relationships in detail when learning SQL JOIN operations.
Before practising the SQL exercises, remember the following points:
dvdrental database must be successfully restored before executing the queries.dvdrental database is separate from the default postgres database.dvdrental before running the queries.public schema.Note: In pgAdmin Query Tool, select the dvdrental database before executing the SQL examples.