Working With Data in PostgreSQL

30 minutes
  • 6 Learning Objectives

About this Hands-on Lab

PostgreSQL is the world’s most advanced open source database. Its stability, functionality, and extensibility make it a primary choice for an RDBMS solution.

In this hands-on lab scenario, you are the DBA for Awesome Company. You have built a PostgreSQL database backend to facilitate the development of a new web application. You will utilize the SQL language to carry out a number of tasks that the development group has requested for the database.

Performing the tasks of this lab will help you become familiar with essential SQL language tasks in PostgreSQL. This includes creating objects, inserting and changing data, as well as retrieving data.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the “acweb” Database and Restore Its Backup
  • Change to the postgres user:
    sudo su - postgres
  • Create the database to be restored:
    createdb acweb
  • Download the backup from GitHub:
    wget https://github.com/linuxacademy/content-postgresql-deepdive/raw/master/acweb/acweb.tar
  • Use pg_restore to restore the backup:
    pg_restore --dbname=acweb --verbose /var/lib/pgsql/acweb.tar
  • Launch psql and verify the data is present:
    psql
    • List the databases with l.
    • Connect to acweb:
      c acweb
    • Get a count for the payment table:
      SELECT COUNT(*) FROM sales.payment;
Create the Table “payment_audit”
  • Use the following query to create the table:
    CREATE TABLE payment_audit (
     payment_id INTEGER,
     customer_id INTEGER,
     amount numeric(6,2),
     payment_date timestamp without time zone NOT NULL,
     username VARCHAR(20),
     delete_date TIMESTAMP
    );
Create the Trigger and Function “audit_payment_deletes”
  • Use the following query to create the needed function:
    CREATE FUNCTION audit_payment_deletes() RETURNS trigger AS $$
     BEGIN
        INSERT INTO payment_audit VALUES((OLD).*, current_user, current_timestamp);
        RETURN OLD;
     END;
    $$ LANGUAGE plpgsql;
  • Use the following query to create a trigger to call this function:
    CREATE TRIGGER audit_payment_deletes
    BEFORE DELETE ON sales.payment 
    FOR EACH ROW
    EXECUTE PROCEDURE audit_payment_deletes();
Create the View “customer_addresses”
  • Use the following query to create the view:
    CREATE VIEW sales.customer_addresses AS
     SELECT c.first_name, c.last_name , a.address, ct.name as city, s.name as state, a.postal_code
     FROM sales.customer c
     LEFT JOIN sales.address a on c.addr_id = a.addr_id
     LEFT JOIN sales.city ct on a.city_id = ct.city_id
     LEFT JOIN sales.state s on ct.state_id = s.state_id;
Run Payments From Mick Fowler
  • Query the sales.customer to find the ID for Mick Fowler:
    SELECT * FROM sales.customer
    WHERE first_name = 'Mick'
    AND last_name = 'Fowler';
  • Using the ID, insert five payments from Mick as directed in the instructions:

    INSERT INTO sales.payment (customer_id, amount, payment_date)
    VALUES (303, 10, current_timestamp);
    
    INSERT INTO sales.payment (customer_id, amount, payment_date)
    VALUES (303, 20, current_timestamp);
    
    INSERT INTO sales.payment (customer_id, amount, payment_date)
    VALUES (303, 30, current_timestamp);
    
    INSERT INTO sales.payment (customer_id, amount, payment_date)
    VALUES (303, 40, current_timestamp);
    
    INSERT INTO sales.payment (customer_id, amount, payment_date)
    VALUES (303, 50, current_timestamp);
  • Change Mick’s $30 payment to $15:
    UPDATE sales.payment
    SET amount = 15
    WHERE customer_id = 303
    AND amount = 30;
  • Delete all of Mick’s payments that are greater than $20:
    DELETE FROM sales.payment
    WHERE customer_id = 303
    AND amount > 20;
Verify That Everything Works As Expected
  • Turn on Expanded Display:
    x
  • Query the customer_addresses view to make sure that the first name, last name, address, city, state, and zip code are displayed:
    SELECT * FROM sales.customer_addresses;
  • Review the payment_audit table. You should see Mick’s payments for $40 and $50 listed:
    SELECT * FROM payment_audit;

Additional Resources

In this hands-on lab scenario you are the DBA for Awesome Company. You have built a PostgreSQL database backend to facilitate the development of a new web application. You will utilize the SQL language to carry out a number of tasks that the development group has requested for the database. This will be accomplished by:

  • Creating a new database called acweb and restoring a backup to it
  • Creating a new table called payment_audit to track deletions on the sales.payment table (capture username, date/time, and the rows deleted)
  • Creating a trigger and function called audit_payment_deletes to carry out auditing deletions on the sales.payment table
  • Creating a view called customer_addresses to display customer first and last names, along with their complete address information
  • Inserting five payments from Mick Fowler that start at $10 and increase in increments of 10 ($10 - $50)
  • Changing the $30 payment from Mick to $15
  • Deleting all payments by Mick that are more than $20
  • Verifying that the addresses view displays correctly and that deletions to sales.payment were successfully audited

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!