Simulating A Real-world SQL Environment
By: James Opacich
Tags: Postgres, Postgresql, Sql

3. Download and Load The Pagila Database

There are two good githubs that I found which house the Pagila Database. I used the Pagila Database by Devrim Gündüz. It was the first one I found that had the database and clear instructions on what it was and how it works. If you are into using containerized environments he also has a tutorial for implementing the database in Docker. In this process I am assuming you already have a github account and know how to use git. If you do not then you can create a github account here and familiarize yourself with github tutorials.

Fork Repository

copy the https or ssh link from the repository to clone it

This will create a forked copy of this repository into your own github repository. It will also navigate the page to your new forked repository.

Clone Repository

You can see in the image that the repository address in the top left of the screen is now my repository. From your forked repository you will clone the repository down to your PC. Click on code and copy the https or SSH url.

Copy the url from the code button. You will insert this into the command line to create a local repository on your own computer.

Start a new command line terminal. You can do this by typing terminal in the search bar on Mac or In the Finder you can open the /Applications/Utilities folder, then double-click Terminal.

Finder

Once terminal is open you can navigate over to the folder/directory where you would like to store the Pagila directory. Learn how to navigate through command line here: Navigate Through Command Line. You will then copy the link into the command line within the desired directory and a new directory will be created that houses all of the pagila database files that were housed in github.

I chose to save my directory into a folder/directory that I call projects.

The ```ls``` command lists out all the files in my new Pagila directory.

At this point you will now have a Pagila directory that houses the Pagila databases on your own computer. We will not pull the Pagila files into PostgreSQL server using pgAdmin 4. Before doing this I strongly recommend reading the README.md file in Devrim Gündüz’s Pagila github repository.

Create a New Database in pgAdmin 4

Right click on “Databases”, click “Create” and then “Database”.

Name your database, I named mine “test_pagila.” Click on the save button.

At this point a new database should show up under the Databases heading in the left broswer. Click on the new database (in my case it is test_pagila), then click on the query tool, go into the new query editor and find the file icon in the top left of the editor and then click on that to search for your Pagila SQL files.

These are the steps you will take to find your Pagila SQL files.

Choose the pagila-insert-data.sql file and click select

The schema and data for the database will autofill into the query editor. Click the run icon and you should see a message that says “ALTER TABLE” below in the Data Ouput box.

Now your database has been created. You can run queries and use this for whatever purposes you now desire. Happy querying!

4. Resources for Using Pagila

There are many ways to train with The Pagila Database. You can spin up your own queries and use it any way you would use a real-world database or you can use question sets that quiz you on your abilities to query using realistic data and data-structures. This repository from Robert Treat is extremely helpful. It contains Pagila setup info and files as well as many links to Pagila tutorials to various tasks such as building applications from Pagila data, using REST API, performing email validation, creating reports and building rails to legacy apps.

I have also scoured the internet to find all kinds of questions (with solutions in most cases) pertaining to Pagila and Sakila and compiled them into a group of Jupyter Notebooks. You can use these questions to test your abilities as well. Find them here:

I also created an Entity Relationship Diagram (ERD) using Pagila. You can find my tutorial for this here: Creating a SQL Entity Relationship Diagram (ERD) and the actual ERD image for your reference below:

Pagila ERD generated by pgAdmin 4

Also, I garnered much of my information to perform this task from these sites:

I hope you enjoyed this tutorial and it helped you get started with Postgres and Pagila. If you have any questions or spot anything that needs clarification please message me on Medium. You can also find much of this information on Github: SQL_Training_with_Pagila by James Opacich