0% found this document useful (0 votes)
40 views25 pages

Hands-On Lab: Create Tables and Load Data in Postgresql Using Pgadmin

This document provides instructions for using pgAdmin to create tables and load data into a PostgreSQL database. It outlines tasks to create a database and tables, insert data manually, and load data from a CSV file. The goal is to learn how to structure and populate a database using the pgAdmin interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views25 pages

Hands-On Lab: Create Tables and Load Data in Postgresql Using Pgadmin

This document provides instructions for using pgAdmin to create tables and load data into a PostgreSQL database. It outlines tasks to create a database and tables, insert data manually, and load data from a CSV file. The goal is to learn how to structure and populate a database using the pgAdmin interface.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Hands-on Lab: Create Tables and Load Data in PostgreSQL using pgAdmin

Estimated time needed: 20 minutes

In this lab, you will learn how to create tables and load data in the PostgreSQL database service using the pgAdmin graphical user interface (GUI) tool. The pgAdmin
GUI provides an alternative to the command line for interacting with a PostgreSQL database using a graphical interface. This GUI provides a number of key features
for interacting with a PostgreSQL database in an easy to use format.

Software used in this lab


In this lab, you will use PostgreSQL Database. PostgreSQL is a Relational Database Management System (RDBMS) designed to efficiently store, manipulate, and
retrieve data.

To complete this lab you will utilize the PostgreSQL relational database service available as part of IBM Skills Network Labs (SN Labs) Cloud IDE. SN Labs is a
virtual lab environment used in this course.

Database used in this lab


You will use the Books database in this lab.

The following diagram shows the structure of the "myauthors" table from the Books database:

Objectives
After completing this lab, you will be able to use pgAdmin with PostgreSQL to:

Create databases and tables in a PostgreSQL instance


Load data into tables manually using the pgAdmin GUI
Load data into tables from a text/script file

Lab Structure
In this lab, you will complete several tasks in which you will learn how to create tables and load data in the PostgreSQL database service using the pgAdmin
graphical user interface (GUI) tool.

Task A: Create a database


First, to create a database on a PostgreSQL server instance, you'll first launch a PostgreSQL server instance on Cloud IDE and open the pgAdmin Graphical User
Interface.

1. Click SKILLS NETWORK extension on the left side of the window.

2. Open the DATABASES drop down menu and click PostgreSQL.

3. Click Start. PostgreSQL may take a few moments to start.


4. Next, open the pgAdmin Graphical User Interface by clicking pgAdmin in the Cloud IDE interface.
5. Once the pgAdmin GUI opens, click Servers tab on the left side of the page. You will be prompted to enter a password.
6. To retrieve your password, click PostgreSQL tab near the top of the interface.

7. Click the Copy icon on the left of your password to copy the session password onto your clipboard.
8. Navigate back to the pgAdmin tab and paste in your password, then click OK.

9. You will then be able to access the pgAdmin GUI tool.


10. In the tree-view, expand Servers > postgres > Databases. If prompted, enter your PostgreSQL service session password. Right-click on Databases and go to
Create > Database. In the Database box, type Books as the name for your new database, and then click Save. Proceed to Task B.
Task B: Create tables
Now that you have your PostgreSQL service active and have created the Books database using pgAdmin, let's create a few tables to populate the database and store
the data that you wish to eventually upload into it.

1. In the tree-view, expand Books > Schemas > public. Right-click on Tables and go to Create > Table.
2. On the General tab, in the Name box, type myauthors as name of the table. Don't click Save, proceed to the next step.

3. Switch to the tab Columns and click the Add new row button four times to add 4 column placeholders. Don't click Save, proceed to the next step.
4. Enter the myauthors table definition structure information as shown in the image below in the highlighted boxes. Then click Save. Proceed to Task C.
Task C: Load data into tables manually using the pgAdmin GUI
You now have a database and have created tables within it. With the pgAdmin GUI, you can insert values into the tables manually. This is useful if you have a few
new entries you wish to add to the database. Let's see how to do it.

1. In the tree-view, expand Tables. Right-click myauthors and go to View/Edit Data > All Rows.
2. You will insert 2 rows of data into the myauthors table. In the lower Data Output pane, enter myauthors table data information for 2 rows as shown in the
highlighted boxes in the image below. Then click the Save Data Changes icon. Proceed to Task D.

Task D: Load data into tables using a text/script file


In the previous task, you entered some data entries into a table manually with pgAdmin. While this method can be useful for small additions, if you wish to upload
large amounts of data at once, the process becomes tedious. An alternative is to load data into tables from a text or script file containing the data you wish to enter.
Let's take a look at how to do this.
1. You will import the remainder of the myauthors table data from a csv text file. Download the csv file below to your local computer:

myauthors.csv

2. In the tree-view, right-click on myauthors and go to Import/Export.


3. Follow the instructions below to import:

Make sure Import/Export is set to Import, Format = csv and Header = Yes. Then click Select file icon by the Filename box.

Click Upload File.


Double-click on the drop files area and load the myauthors.csv you downloaded earlier from your local computer.
When the upload is complete, close the drop files area by clicking X.
Select the uploaded myauthors.csv file from the list and click Select.
Click OK and notification of import success will appear.
4. Repeat Task C Step 1 to check that the newly imported data rows appear along with your previously inserted 2 rows.
As you can see, the data contained in the csv file was successfully uploaded into the table and you did not have to manually input hundreds of entries.

Conclusion
Congratulations! You have completed this lab, and you have learned how to create databases and tables in a PostgreSQL instance, load data into tables manually
using the pgAdmin GUI, and load data into tables from a text/script file.

Author
Sandip Saha Joy

Other Contributors
David Pasternak

© IBM Corporation. All rights reserved.

You might also like