Skip to main content.

Using PostgreSQL in Docker

Objectives

Review: Docker Commands

Our Docker Exercises

Using Docker to Run PostgreSQL

  1. Create a directory for this exercise/demo.
  2. Download the files in postgresql into your directory. Recall that Dockerfile should not have an extension.
  3. View the contents of both files. What does the Dockerfile do? What does clubdata.sql contain?
  4. Build the image from the Dockerfile, naming the image something like psql
  5. Run the container and name it something like psql. (Yeah, they can be the same.)

Practicing with PostgreSQL

Connect to the database (within the container)

  1. Get a command-line interface/shell for the container.
  2. Run psql exercises

    What is exercises referring to?

    You should get an error because there is no root user. (If you get a different error, let me know.) Do you know a user name? What about the password?

  3. Run psql -U <username> -W exercises where username is the database's username.
  4. Enter the password. (Full disclosure, you can also access the database if you don't use the -W option for the password.)
  5. You're now using PostgreSQL!

Using the database

Let's find out more about the database!

  1. \d
  2. \d facilities

    These commands do not require ; They are not SQL commands but ways to get information about the database. These commands are different in other DBMSs.

  3. Use the above command and look at the other two tables too.
  4. How are the tables related? What information can we get from the tables?
  5. Where was this information in the clubdata.sql file?

Now, let's look at the data!

  1. Get all the information about the facilities

    select * from facilities;

  2. Get all the information about the members.
  3. Get all the information about the bookings.
  4. How do I get the bookings for one specific member?
  5. How do I get the bookings for one specific member and also see the information about the member?
  6. How do I also see what facilities those are in?
  7. How do I see this information for all members?

Now, let's add some data!

  1. Add yourself as a customer.
  2. Add DARC as a new facility.
  3. Create a new booking for yourself at DARC.