Answer Key For PostgreSQL Essential v16
Answer Key For PostgreSQL Essential v16
PostgreSQL Essential – 16
Table of Contents
Module 3 - PostgreSQL Installation .......................................................................... 4
Lab Exercise - 1 .................................................................................................... 4
Answer Lab Exercise 1 (RHEL/Rocky/OL Linux 9) ............................................ 4
Module 4 - User Tools - Command Line Interfaces................................................... 5
Lab Exercise – 1.................................................................................................... 5
Answer Lab Exercise – 1 ................................................................................... 5
Module 5 – Database Clusters .................................................................................. 8
Lab Exercise – 1.................................................................................................... 8
Answer - Lab Exercise-1 .................................................................................... 8
Module 6 - Configuration ......................................................................................... 10
Lab Exercise - 1 .................................................................................................. 10
Answer Lab Exercise -1 ................................................................................... 10
Lab Exercise - 2 .................................................................................................. 10
Answer Lab Exercise – 2 ................................................................................. 11
Lab Exercise - 3 .................................................................................................. 12
Lab Exercise - 4 .................................................................................................. 12
Module 7 - Data Dictionary ..................................................................................... 14
Lab Exercise - 1 .................................................................................................. 14
Answer Lab Exercise - 1 .................................................................................. 14
Lab Exercise - 2 .................................................................................................. 14
Answer Lab Exercise - 2 .................................................................................. 14
Lab Exercise - 3 .................................................................................................. 15
Answer Lab Exercise - 3 .................................................................................. 15
Lab Exercise - 4 .................................................................................................. 17
Answer Lab Exercise - 4 .................................................................................. 17
Module 8 - Creating and Managing Databases ....................................................... 18
Lab Exercise - 1 .................................................................................................. 18
Lab Exercise - 2 .................................................................................................. 19
Answer Lab Exercise - 2 .................................................................................. 19
Module 9 - Security ................................................................................................. 21
Lab Exercise - 1 .................................................................................................. 21
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |1
Answer Lab Exercise - 1 .................................................................................. 21
Lab Exercise - 2 .................................................................................................. 21
Answer Lab Exercise - 2 .................................................................................. 21
Lab Exercise - 3 .................................................................................................. 22
Answer Lab Exercise - 3 .................................................................................. 22
Module 10 – Monitoring and Admin Tools ............................................................... 23
Lab Exercise - 1 .................................................................................................. 23
Answer Lab Exercise - 1 .................................................................................. 23
Lab Exercise - 2 .................................................................................................. 28
Answer Lab Exercise - 2 .................................................................................. 28
Lab Exercise - 3 .................................................................................................. 32
Answer Lab Exercise - 3 .................................................................................. 32
Lab Exercise - 4 .................................................................................................. 33
Answer Lab Exercise - 4 .................................................................................. 33
Module 11 - SQL Primer ......................................................................................... 35
Lab Exercise - 1 .................................................................................................. 35
Answer Lab Exercise - 1 .................................................................................. 35
Lab Exercise - 2 .................................................................................................. 35
Answer Lab Exercise - 2 .................................................................................. 36
Lab Exercise - 3 .................................................................................................. 36
Answer Lab Exercise - 3 .................................................................................. 36
Module 12 - Backup Recovery ................................................................................ 37
Lab Exercise - 1 .................................................................................................. 37
Answer Lab Exercise - 1 .................................................................................. 37
Lab Exercise - 2 .................................................................................................. 38
Answer Lab Exercise - 2 .................................................................................. 38
Lab Exercise - 3 .................................................................................................. 38
Answer Lab Exercise - 3 .................................................................................. 39
Lab Exercise - 4 .................................................................................................. 39
Answer Lab Exercise - 4 .................................................................................. 39
Lab Exercise - 5 .................................................................................................. 41
Answer Lab Exercise - 5 .................................................................................. 41
Module 13 - Routine Maintenance Tasks................................................................ 43
Lab Exercise – 1.................................................................................................. 43
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |2
Answer Lab Exercise - 1 .................................................................................. 43
Lab Exercise - 2 .................................................................................................. 44
Answer Lab Exercise - 2 .................................................................................. 44
Module 14 - Moving Data ........................................................................................ 45
Lab Exercise - 1 .................................................................................................. 45
Answer Lab Exercise - 1 .................................................................................. 45
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |3
Answer Key for Foundations
PostgreSQL Administration Labs – 15
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |4
Module 4 - User Tools - Command Line Interfaces
Lab Exercise – 1
1. Connect to a database using psql.
2. Switch databases.
3. Describe the customers table.
4. Describe the customers table including description.
5. List all databases.
6. List all schemas.
7. List all tablespaces.
8. Execute a sql statement, saving the output to a file.
9. Do the same thing, just saving data, not the column headers.
10. Create a script via another method and execute from psql.
11. Turn on the expanded table formatting mode.
12. Lists tables, views and sequences with their associated access privileges.
13. View the current working directory.
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |5
Type the following command:
\o customer_data.txt
SELECT * FROM customers;
\o
9. Do the same thing, just saving data, not the column headers.
Type the following command:
\t
\o customer_data.txt
SELECT * FROM customers;
\o
\t
10. Exit out of psql then create a script via another method, and execute from
psql.
a. Exit .Type \q
b. Create the sql file.
Type:
i. vi emp.sql
ii. SELECT * FROM emp;
iii. <ESC>:wq <Enter>
12. Lists tables, views, and sequences with their associated access
privileges.
Type the following command: \dp
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |6
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |7
Module 5 – Database Clusters
Lab Exercise – 1
1. A new website is to be developed for an online music store
– Create a new cluster with data directory /edbdata and ownership of
postgres user
– Start your edbdata cluster
– Reload your cluster with pg_ctl utility and using pg_reload_conf()
function
– Stop your edbdata cluster with fast mode
Note: Answer of this Lab Exercise is given assuming PATH variable is set
in .bash_profile or can be set using export PATH=/usr/pgsql-
16/bin:$PATH
1. Open a terminal window. Type sudo mkdir /edbdata then enter the
sudo user password.
Type command:
sudo mkdir /edbdata
2. Change the ownership to postgres user.
Type the following command:
sudo chown postgres:postgres /edbdata
3. Login as postgres.
Type su – postgres and enter the postgres user password
6. Change the port to 5433 . Uncomment the parameter and set the port
port = 5433
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |8
Type pg_ctl -D /edbdata start
©EnterpriseDB® Corporation. All rights reserved Answer Key PostgreSQL Essentials 16 Page |9
Module 6 - Configuration
Lab Exercise - 1
1. You are working as DBA. It is recommended to keep a backup copy of
postgresql.conf file before making any changes. Make necessary changes in
server parameter file for following settings:
– Allow up to 200 connected users
– Reserve 10 connection slots for DBA
– Set the maximum time to complete client authentication to be 10 seconds
Lab Exercise - 2
1. Working as a DBA is a challenging job and to track down certain activities on the
database server logging has to be implemented. Go through the server
parameters that control logging and implement the following:
Lab Exercise - 4
1. Vacuuming is an important maintenance activity and needs to be properly
configured. Change the following autovacuum parameters on the production
server. Set:
– Autovacuum workers to 6
– Autovacuum threshold to 100
– Autovacuum scale factor to 0.3
– Auto analyze threshold to 100
– Autovacuum cost limit to 100
Open the postgresql conf file. Type <i> to go into insert mode
vi /var/lib/pgsql/16/data/postgresql.conf
2. Write a query to find the list of schemas currently in your search path. Type
SHOW search_path;
Lab Exercise - 2
1. You need to determine the names and definitions of all the views in your
schema. Create a report that retrieves view information - the view name and
definition text.
Lab Exercise - 3
1. Create report of all the users who are currently connected. The report must
display total session time of all connected users.
2. You found that a user has connected to the server for a very long time and have
decided to gracefully kill its connection. Write a statement to perform this task.
5. Create report of all the users who are currently connected. The report must
display total session time of all connected users.
Type the following command:
SELECT usename,now()-backend_start AS "Total Connection
Time", pid FROM pg_stat_activity;
7. You found the the edbuser user has been connected to the server for too
long. Gracefully kill edbuser’s connection. Write a statement to perform this
task.
Type the following command:
SELECT pg_terminate_backend(edbuser pid);
Lab Exercise - 4
1. Write a query to display the name and size of all the databases in your cluster.
Size must be displayed using a meaningful unit.
1. Write a query to display name and size of all the databases in your cluster.
Size must be displayed using a meaningful unit.
Lab Exercise - 2
1. Retrieve a list of databases using a SQL query.
2. Retrieve a list of databases using the psql meta command.
3. Retrieve a list of tables in the edbstore database and check which schema
and owner they have.
3. Retrieve a list of tables in edbstore database and check which schema and
owner they have. Type \dt
Lab Exercise - 2
1. A new developer has joined the team, and his ID number is 89.
– Create a new user by name dev89 and password password89.
– Then assign the necessary privileges to dev89 so that he can connect to
the edbstore database and view all tables.
4. Grant select, insert, delete and update on all the edbuser tables.
Type the following command:
GRANT SELECT, INSERT, DELETE, UPDATE ON ALL TABLES IN
SCHEMA edbuser to dev89;
Lab Exercise - 3
1. A new developer joins e-music corp. They have an IP address 192.168.30.89.
They are not able to connect from his machine to the Postgres server and gets
the following error on the server:
d. Click the Privileges tab and set Can login to Yes then click Save.
c. Click Columns tab and enter the following column names and data
types then click Save. For each new column click the add button in
the right corner.
i. TeamID, numeric
ii. TeamName, character varying
b. Go to Create: Check
7. Create a view that will display all teams in ascending order of their ratings.
Name the view as vw_top_teams
a. Right click on Views in the pguser Schema and click Create: View
Lab Exercise - 3
1. View all rows in the Teams table.
2. Using the Edit data window you just opened in the previous step, insert the
following rows into the Teams table:
2. In the Data Output window from previous step insert the above rows to the
Teams table.
a. Double click each row TeamName and enter the value, then double click
TeamRatings and enter the value.
b. Click the Save button.
Lab Exercise - 4
1. Connect to the pgdb database using the query tool.
2. Using the graphical query builder retrieve all the rows present in the Teams table.
3. Using the graphical query builder retrieve all the rows present in view
vw_top_teams.
6. Go to the File Menu and choose Exit. Click No to save changes message.
2. True
5. Case sensitive Colum Headings and Column Headings with space must be
enclosed in double quotes "
SELECT empno, ename, sal * 12 "ANNUAL SALARY" FROM
edbuser.emp;
Lab Exercise - 2
1. The staff in the HR department wants to hide some of the data in the EMP table.
They want a view called EMPVU based on the employee numbers, employee
names, and department numbers from the EMP table. They want the heading for
the employee name to be EMPLOYEE.
2. Confirm that the view works. Display the contents of the EMPVU view.
3. Using your EMPVU view, write a query for the SALES department to display all
employee names and department numbers.
\dv
Lab Exercise - 3
1. You need a sequence that can be used with the primary key column of the dept
table. The sequence should start at 60 and have a maximum value of 200.
Have your sequence increment by 10. Name the sequence dept_id_seq.
2. To test your sequence, write a script to insert two rows in the dept table.
3. Take a full database dump of the edbstore database with the pg_dump
utility. The dump should be in plain text format. Name the dump file as
edbstore_full.sql and store it in the /pgbackup directory.
Lab Exercise - 2
1. Take a dump of the edbuser schema from the edbstore database and name
the file as edbuser_schema.sql
2. Take a data-only dump of the edbstore database, disable all triggers for faster
restore, use the INSERT command instead of COPY, and name the file as
edbstore_data.sql
3. Take a full dump of customers table and name the file as
edbstore_customers.sql
2. Take a dump of the edbuser schema from the edbstore database and
name the dump file edbuser_schema.sql.
Type the following command:
pg_dump -n edbuser -f /pgbackup/edbuser_schema.sql -U
postgres edbstore then type the postgres password.
3. Take a data-only dump of the edbstore database, disable all triggers for a
faster restore, and use the insert command instead of copy, then name the
file as edbstore_data.sql.
Lab Exercise - 3
1. Take a full database dump of edbstore in compressed format using the
pg_dump utility, name the file as edbstore_full_fc.dmp
1. Take a full database dump of the edbstore in compressed format using the
pg_dump utility, name the file as edbstore_full_fc.dm.
Type the following command:
pg_dump -Fc -f /pgbackup/edbstore_full_fc.dmp -U
postgres edbstore then type the postgres password.
Lab Exercise - 4
In this exercise you will demonstrate your ability to restore a database.
1. Drop database edbstore.
2. Create database edbstore with owner edbuser.
3. Restore the full dump from edbstore_full.sql and verify all the objects
and their ownership.
4. Drop database edbstore.
5. Create database edbstore with edbuser owner.
6. Restore the full dump from the compressed file edbstore_full_fc.dmp
and verify all the objects and their ownership.
3. Restore the full dump from edbstore_full.sql and verify all the objects and
their ownership.
Type the following command:
psql -f /pgbackup/edbstore_full.sql -d edbstore -U
edbuser then type the password for edbuser.
6. Restore the full dump from compressed file edbstore_full_fc.dmp and verify
all the objects and their ownership.
Type the following command:
pg_restore -d edbstore /pgbackup/edbstore_full_fc.dmp
Lab Exercise - 5
1. Create a directory /opt/arch or c:\arch and give ownership to the
postgres user.
2. Configure your cluster to run in archive mode and set the archive log location to
be /opt/arch or c:\arch.
3. Take a full online base backup of your cluster in the /pgbackup directory using
the pg_basebackup utility.
1. Write a SQL script to perform maintenance on the customers, emp, dept and
orders tables.
Type the following command:
vi edbstore_mnts_script.sql to create the script.
Then type <i> to get into insert mode and enter the following maintenance
commands:
VACUUM FULL customers;
ANALYZE emp;
ANALYZE dept;
VACUUM orders;
3. Load the csv file (from step 1) into the copyemp table.
1. Unload emp table from edbuser schema to a csv file, with column headers.
Type the following command:
COPY edbuser.emp to '/home/postgres/emp.csv' with csv
header;
Review the data in the copyemp table. Type SELECT * FROM copyemp;