# Once the server is started - Download the Script
//cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0231EN-SkillsNetwork/labs/Final%20Assignment/postgres-setup.sh wget https:
Script Files Details
User Management - PostgreSQL
Scenario
For this project you will assume the role of database administrator at a data analytics consulting company. You have been assigned to a project where you need to setup, test and optimize the data platform. The platform includes various on-premises database servers like MySQL, PostgreSQL, and an open-source tool such as Datasette, as well as optional cloud-based databases like IBM DB2. Your job is to configure, tune, secure, backup and monitor those databases and keep them running at peak performance.
Project
Let’s pretend you’re the DBA so you’ll need to perform user management tasks and handle the backup of the databases on a PostgreSQL server
Tasks
- Installation & Provisioning
- Configuration
- User Mangement
- Backup
Setup
We will be using an open-source cloud IDE platform to access the PostgreSQL database which is running in a Docker container.
Database Script
Download db Script
- Start the PostgreSQL Server - Create an instance on the cloud
- Download the db setup file from: https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0231EN-SkillsNetwork/labs/Final%20Assignment/postgres-setup.sh
Here is what the bash file contained
# Here is the content of the .sh file:
#download the data file
//cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0231EN-SkillsNetwork/labs/Final%20Assignment/vehicle-data.csv
wget https:
#download the sql file
//cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0231EN-SkillsNetwork/labs/Final%20Assignment/setup.sql
wget https:
#run the sql file
--username=postgres --host=localhost -f setup.sql
psql
#import the csv file
-data.csv | psql --username=postgres -d tolldata --host=localhost -c "copy toll.tolldata from STDIN WITH (FORMAT csv);" cat vehicle
Here is the setup.sql script
;
CREATE DATABASE tolldata;
\c tolldata;
CREATE SCHEMA toll
CREATE TABLE toll.tolldata(
row_id integer,25),
timestamp varchar(
vehicle_id integer,10),
vehicle_type varchar(
payment_type integer,10),
category_id varchar(
primary key (row_id); )
Here is part of the data file vehicle-data.csv
1,Thu Aug 19 21:54:38 2021,125094,car,2,VC965
2,Sat Jul 31 04:09:44 2021,174434,car,2,VC965
3,Sat Aug 14 17:19:04 2021,8538286,car,2,VC965
4,Mon Aug 2 18:23:45 2021,5521221,car,2,VC965
5,Thu Jul 29 22:44:20 2021,3267767,car,2,VC965
6,Sat Aug 14 03:57:47 2021,8411850,car,2,VC965
7,Thu Aug 12 03:41:22 2021,6064250,car,2,VC965
- Once the bash script is executed all those files are downloaded and some are created
- Open PostgreSQL CLI
Execute Bash file
# Create Database by executing the bash file or wait and see section Create db from script below
-setup.sh bash postgres
Create db from script
- Create db from setup.sql
- Run the setup.sql file
- Or execute .sh file as shown above
=# postgres=# \i setup.sql
postgres
CREATE DATABASE14.13 (Ubuntu 14.13-0ubuntu0.22.04.1), server 13.2)
psql ("tolldata" as user "postgres".
You are now connected to database
CREATE SCHEMA
CREATE TABLE-# tolldata
It still didn’t load the data in the table
- Opened pgAdmin
- Left file tree > Expand PostgreSQL
- Scroll down to Database: tolldata
- Schemas > Expand > toll (table name)
- Scroll down to Tables > Scroll down to tolldata
- Right click > Import/Export Data
- Import
- Format csv
- Filename: Click on folder > either go to var/lib/pgadmin or choose the unlocked /tmp directory
- Choose the … dots on the right side
- Import > file vehicle-data.csv from local or from server
- Might have to wait till it populates the directory > Select or go back to the Import screen if it takes time, till the Select button is active.
- Select and wait till you receive a confirmation that data is loaded
Task 1 - Max Connections
Using CLI
- Find the configuration file: postgresql.conf
- For some reason the file is nowhere to be found so
- I edited it’s permission with
chmod +x postgres-setup.sh
- Still didn’t appear in the file tree
- Will try to open it with nano :
sudo nano /var/lib/postgresql/data/pgdata/postgresql.conf
=# SHOW config_file;
postgres
config_file -------------------------------------------------
/var/lib/postgresql/data/pgdata/postgresql.conf
1 row)
(
=# SHOW max_connections;
postgres
max_connections -----------------
100
1 row) (
From pgAdmin
Task 2 - User Management
Create User
Create a user named backup_operator
=# CREATE USER backup_operator WITH PASSWORD 'backup_operator_password';
postgres CREATE ROLE
Create Role
Create a role named backup
=# CREATE ROLE backup;
postgres CREATE ROLE
Grant Privileges to Role
Grant the following privileges to the role backup
- CONNECT ON tolldata DATABASE.
- SELECT ON ALL TABLES IN SCHEMA toll.
=# GRANT CONNECT ON DATABASE tolldata TO backup;
postgres=# GRANT SELECT ON ALL TABLES IN SCHEMA toll TO backup; postgres
Grant Role to User
Grant the role backup to backup_operator
=# GRANT backup TO backup_operator;
postgres GRANT ROLE
Task 3 - Backup
Backup the database using PGADMIN
Backup the database tolldata into a file named tolldatabackup.tar, select the backup format as Tar
- Open pgAdmin
- Scroll down to tolldata
- Right click on it and backup to a tar tolldatabackup.tar file
Db2 Duplicate
- These images are of the same process done in Db2 cloud server