postgresql-admin-lab-v3.5-English
postgresql-admin-lab-v3.5-English
1.1 ) Login to the lab machine using SSH and switch the user to root using the following command :
sudo su -
Note: You can execute the commands in Step 1.2.1 to 1.2.3 using “/pg-lab-install/install-
pg.sh” script.
1.2.2 ) Run dnf command to prevent PostgreSQL installation from Redhat/Rocky Linux build-in
repository:
Check whether the directory /usr/pgsql-15/bin has been created and verify that files have been
installed under this directory.
2.1) Login to the lab machine using SSH and switch the user to root using the follwing command :
sudo su -
mkdir /data
chown postgres:postgres /data
chmod 700 /data
su – postgres
Then, use initdb command to initialize the directory with the following command :
/usr/pgsql-15/bin/initdb -D /data
2.5) Use the followin command to check if Postgres service has started.
Next, use the command from step 2.5) to verify if the postgres service is running.
Note: If you left PostgreSQL service running in the previous lab, please stop it using “pg_ctl
stop” command as mention in step 2.6) before proceeding.
3.1) Login to the lab machine using ssh and switch user to root.
sudo su -
3.2) Edit the file using vi or nano.
Environment=PGDATA=/data
systemctl daemon-reload
3.5) Verify if postgres is running after using command in step 4.4) by executing the command below :
As this command output, if PostgreSQL service is running, the line staring with Active: should be
display status as “active (running)” as shown in the following example:
After the Linux server has rebooted, login and switch user to root (as step 3.1). Then use the command
as in step 3.5 to check if Postgres service get started automatically.
4.1) Login to the lab machine using ssh and switch user to root.
sudo su -
su – postgres
4.3) Modify /var/lib/pgsql/.pgsql_profile ( with dot as the first character in the
filename) using either nano or vi.
nano .pgsql_profile
-or-
vi .pgsql_profile
export PATH=$PATH:/usr/pgsql-15/bin
export PGDATA=/data
4.4) Logout and relogin lab machine. Then, try running ‘pg_ctl status’ command as in steps 2.5
with command name only (without path name) and without “-D /data” option.
5.1) Login to the lab machine using ssh client and switch user to postgres.
sudo su – postgres
5.2) Connect to Postgres service using psql command (no option required).
5.3) While in psql command, use "\l" command to list databases created in this system.
Please list the database name you see : __________
5.4) Then connect the database named “postgres” using the following command :
5.5 ) Check the current database you are connecting to with this command :
select current_database();
5.6) Exit from psql using ‘\q’ command to return to Linux command line.
5.7) Then execute exit command to quit from postgres account and return to previous Linux account
that you was logged into.
5.8) Question : While you are logging in lab server as Linux account other then postgres, if you want
to use psql command connect to Postgres server as in step 5.2), how to run psql command ? (What psql
options to specify while running psql ? )
___________________________________________
6.1) Run psql and list existing databases. What is psql command to do that ? …..........
6.2) Create a new database named mydatabase. What command should be executed ?…….
6.3) Use psql command to list databases again. Do you see a new database created in step 6.2) ?
6.4) To drop the database created in step 6.2) What is the command to do that ?........................
Then list currently existing databases again to check if mydatabase database has been removed ?
Connect to PostgreSQl using psql command and proceeded with following steps :
7.3) If you want to list the created table. What command will you use ? _________
7.4) In the ‘testdb’ database create in 7.1), create a schema named “myschema”. Then, use psql
7.5) Create a new table in the schema created in step 7.4) using the followin command :
7.6) Then use command ‘\dt myschema.*’ to list to list the created table in 7.5)
8.1) Login to the lab machine using ssh client and switch user to postgres.
(use “sudo su – postgres” command)
8.2) Create a new PostgreSQL user named “pgadmin” with the following requirements :
◦ Assign password as "pgadminpass"
◦ Assign option as superuser
What command should be use ? .........
8.3) Use psql command to connect to postgres as the “pgadmin” user. How do you execute psql
command (and options) ? …......
Then use the following command to check which Postgres user you are using :
select current_user;
8.4) While connecting to Postgres as the pgadmin user, create a new Postgres user with the
following requirements :
8.5) Exit from psql and then reconnect Postgres using psql command with user1.
Lab 9 : pg_hba.conf
Note: We will use Postgres users named pgadmin and user1 from Lab 7.
9.1) Login (ro ssh) to lab machine and switch user to ‘postgres’ (use “sudo su – postgres”
command)
listen_addresses = '*'
9.3) Modify /data/pg_hba.conf to allow Postgres clients using pgadmin account to connect
to postgres from any IP adresses, connect to any databases, and require specifing password.
Note : After modifying pg_hba.conf , you must restart or reload Postgres service.
9.4) Find the IP address beginning with 10.15. of you lab machine with the following command :
hostname -I
For example :
9.5) Test what you did in step 9.3) Using psql command as following syntax :
Substitute ip_address with IP address of you lab server obtained from step 9.4) and 127.0.0.1 .
If correct, you should be able to connect to Postgres using both IP addresses.
Lab 10 : Tablespace
Objective : Learn how to create a Tablespace and create tables in Tablespace.
mkdir /my_tablespace_dir
chown postgres:postgres /my_tablespace_dir
10.2) Switch user to postgres (using “su – postgres”) and run the psqlcommand.
10.3) While in psql, create a new tablespace named "my_tablespace” on the directory created in
step 10.1) using the follow command:
List the newly create tablespace with “\db” command and check if it is created in the specifed
directory.
10.4) Create lab_tbspace database and connect using the following commands :
10.5) Create table1 table in lab_tbspace database (the default tablespace) using the command :
10.6) Create table2 table in my_tablespace tablespace in lab_tbspace database using the
Examine the output of the command above and verify table and tablespace names at tablename and
tablespace columns respectively. Confirm if they were created using the command in steps 10.5 and
10.6
cd /pg-lab-install
cp *.sql /var/lib/pgsql
cp sample_superstore.csv /var/lib/pgsql
cd /var/lib/pgsql
chown postgres:postgres sample_superstore.csv
chown postgres:postgres *.sql
createdb testdb
11.3) Create superstore table in testdb database with the following command :
psql -f /var/lib/pgsql/create_superstore.sql
11.4.2) Count the number of records in the superstore table and note down the result.
If you run the COPY command correctly , the output you should see is :
COPY 9994
11.6) Run the same command as step 11.4.2 to count the number of records in superstore table
again. How many records ? …...................
11.7) Use COPY command to export data in superstore table in text file format using the following
command :
Note: In this step, you must specify filename with full pathname.
11.8) Exit from psql back to Linux command line. Then, check the format of data in
/var/lib/pgsql/sample_superstore_nohead.txt file with the following
command :
head /var/lib/pgsql/sample_superstore_nohead.txt
sudo su – postgres
12.3) Use pg_dump command to backup sample_db database to sample_db.dump file using
following command:
createdb sample2
12.5) Then, restore the backup file created in 12.3) to “sample2” database
Question: Is the data in the tables of both databases are different ? And why ? …........…
13.1) Login Linux and then switch user to root with the command :
sudo su -
13.2) Install postgresql15-contrib package if it has not been installed before with following
command :
dnf install postgresql15-contrib
13.9) Use '\dx’ command to list installed extension again. Do you see dblink extension ? Why ?
Objective : Learn how to use PostgreSQL build-in functions to read PostgreSQL parameters and
assigned values
14.1) login Linux and switch user to postgres. Use the following commands :
sudo su - postgres
psql
select current_setting('shared_buffers');
14.4) Read current shared_buffers value from pg_settings table using the command :
sudo su – postgres
/usr/bin/psql
select pg_sleep('1000000');
The result will show many records. Find the record which has the query column as
pg_sleep('1000000'). Write down the value in pid column of this record.
pid is _________.
15.4) In the second session, cancel the postgres backend process which run pg_sleep(). Use the
command with the following syntax :
select pg_cancel_backend('pid_number');
Observe what happens to the first session running ' select pg_sleep('1000000');'
select pg_sleep('3000000');
15.6) In the second session, run command with the following syntax:
select pg_terminate_backend('pid_number');
Replace pid_number as the pid value obtained from 15.5). What will happen to the first session after
executing the command?
15.7) Running the query below to get pid value of the first session:
16.1) At Linux command line , switch user to postgres and edit /data/postgresql.conf
sudo su – postgres
vi /data/postgresql.conf
work_mem = 8MB
Recored the new value. Don't restart Postgres service at this step.
psql
select current_setting('work_mem');
select pg_reload_conf();
Question: If we use the same steps as 16.1) to 16.3) to change the value of shared_buffers. Can
we do that ? If not , why ?
17.1) login and switch user to postgres using the following command:
sudo su – postgres
17.2) Run psql and connect to testdb database created in previous lab :
psql testdb
17.3) Find the file name storing superstore table using command :
select pg_relation_filepath('superstore');
select pg_table_size('superstore');
select pg_total_relation_size('superstore');
17.6) Exit from psql using ‘\q’ command to return to Linux command prompt. Then, use
‘ls -l’ command to verify if the file name you obtained from step 17.3) really exists. Additionaly,
compare the size of file obtained from this step with the size from 17.4).