Module 03 · MySQL and PostgreSQL
Hands-on Lab: Create Tables and Load Data in PostgreSQL using pgAdmin动手实验:使用 pgAdmin 在 PostgreSQL 中建表与加载数据
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.
在这个实验中,你将学习如何使用 pgAdmin 图形用户界面(GUI)工具在 PostgreSQL 数据库服务中创建表和加载数据。pgAdmin GUI 为使用图形界面与 PostgreSQL 数据库交互提供了命令行的替代方案。该 GUI 以易于使用的方式提供了一系列关键功能,用于与 PostgreSQL 数据库进行交互。
Estimated time needed: 20 minutes
预计所需时间:20 分钟
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.
在这个实验中,你将使用 PostgreSQL 数据库。PostgreSQL 是一种关系型数据库管理系统(RDBMS),设计用于高效地存储、操作和检索数据。
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.
要完成这个实验,你将利用 IBM Skills Network Labs(SN Labs)云 IDE 中提供的 PostgreSQL 关系型数据库服务。SN Labs 是本课程中使用的虚拟实验室环境。
Database used in this lab 本实验中使用的数据库
You will use the Books database in this lab.
你将在本次实验中使用 Books 数据库。
The following diagram shows the structure of the "myauthors" table from the Books database:
下图展示了 Books 数据库中"myauthors"表的结构:
Objectives 目标
After completing this lab, you will be able to use pgAdmin with PostgreSQL to:
完成此实验后,您将能够使用 pgAdmin 与 PostgreSQL 来:
- Create databases and tables in a PostgreSQL instance
在 PostgreSQL 实例中创建数据库和表 - Load data into tables manually using the pgAdmin GUI
使用 pgAdmin 图形界面手动将数据加载到表中 - 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.
在本实验中,您将完成几个任务,通过这些任务,您将学习如何使用 pgAdmin 图形用户界面(GUI)工具在 PostgreSQL 数据库服务中创建表和加载数据。
Task A: Create a database 任务 A:创建数据库
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.
首先,要在 PostgreSQL 服务器实例上创建数据库,您首先需要在 Cloud IDE 上启动一个 PostgreSQL 服务器实例,并打开 pgAdmin 图形用户界面。
- Click the Skills Network extension button on the left side of the window.
点击窗口左侧的 Skills Network 扩展按钮。 - Open the DATABASES menu and click PostgreSQL.
打开 DATABASES 菜单,然后点击 PostgreSQL。 - Click Create. PostgreSQL may take a few moments to start.
点击创建。PostgreSQL 可能需要几秒钟才能启动。 - Next, open the pgAdmin Graphical User Interface by clicking pgAdmin in the Cloud IDE interface.
接下来,通过在 Cloud IDE 界面中点击 pgAdmin 来打开 pgAdmin 图形用户界面。 - Once the pgAdmin GUI opens, click Servers tab on the left side of the page. You will be prompted to enter a password.
一旦 pgAdmin GUI 打开,点击页面左侧的 Servers 标签页。系统将提示您输入密码。 - To retrieve your password, click PostgreSQL tab near the top of the interface and select Connection Information tab.
要获取您的密码,请点击界面顶部的 PostgreSQL 标签页,并选择连接信息标签页。 - Scroll down and click the Copy icon on the left of your password to copy the session password onto your clipboard.
向下滚动,点击密码左侧的复制图标,将会话密码复制到剪贴板。 - Navigate back to the pgAdmin tab and paste in your password, then click OK.
返回 pgAdmin 标签页,粘贴您的密码,然后点击确定。 - You will then be able to access the pgAdmin GUI tool.
您将能够访问 pgAdmin 图形界面工具。 - 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.
在树形视图中,展开服务器 > postgres > 数据库。如果提示,输入您的 PostgreSQL 服务会话密码。在数据库上右键单击,选择创建 > 数据库。在数据库框中,将新数据库名称输入为 Books,然后点击保存。继续进行任务 B。
Task B: Create tables 任务 B:创建表格
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.
现在您的 PostgreSQL 服务已激活,并使用 pgAdmin 创建了 Books 数据库,让我们创建几个表来填充数据库并存储您希望最终上传到其中的数据。
- In the tree-view, expand Books > Schemas > public. Right-click on Tables and go to Create > Table.
在树形视图中,展开 Books > Schemas > public。右键单击 Tables,然后选择 Create > Table。 - On the General tab, in the Name box, type myauthors as name of the table. Don't click Save, proceed to the next step.
在常规选项卡中,在名称框中输入 myauthors 作为表名。不要点击保存,继续下一步。 - 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.
请将下方高亮框中所示 myauthors 表的定义结构信息输入。然后点击保存。继续进行任务 C。
Task C: Load data into tables manually using the pgAdmin GUI 任务 C:使用 pgAdmin 图形界面手动将数据加载到表中
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.
您现在已经拥有一个数据库,并在其中创建了表。使用 pgAdmin 图形界面,您可以手动向表中插入值。如果您希望向数据库添加少量新条目,这会很有用。让我们看看如何操作。
-
In the tree-view, expand Tables. Right-click myauthors and go to View/Edit Data > All Rows.
在树形视图中,展开“表”。右键单击 myauthors,然后选择“查看/编辑数据 > 所有行”。 -
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.
您将向 myauthors 表插入 2 行数据。在下方“数据输出”窗格中,按照下图高亮框中所示,为 2 行输入 myauthors 表的数据信息。然后单击“保存数据更改”图标。继续进行任务 D。 -
Enter the values into the table as shown below:
按照下图所示,将值输入表中: -
Save the values. 保存数值。
Task D: Load data into tables using a text/script file 任务 D:使用文本/脚本文件将数据加载到表中
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.
在上一项任务中,您使用 pgAdmin 手动将一些数据条目输入到表格中。虽然这种方法对于少量添加数据很有用,但如果您希望一次性上传大量数据,这个过程就会变得繁琐。另一种方法是从包含您希望输入的数据的文本或脚本文件中加载数据到表格中。让我们来看看如何操作。
- You will import the remainder of the myauthors table data from a csv text file. Download the csv file below to your local computer:
您将从一个 csv 文本文件中导入 myauthors 表格的其余数据。将下面的 csv 文件下载到您的本地计算机: - In the tree-view, right-click on myauthors and go to Import/Export.
在树形视图中,右键单击 myauthors,然后选择导入/导出。 - Follow the instructions below to import:
按照以下说明导入:- Make sure Import/Export is set to Import,
确保导入/导出设置为导入, - Format = csv. 格式 = csv。
- Then click Select file icon by the Filename box.
然后点击文件名框旁边的选择文件图标。 - Steps to Upload File. 上传文件的步骤。
- Step 1: Initially make sure the folder details empty and select the var option from the list as shown in the screenshot below. Select var folder
步骤 1:首先确保文件夹详细信息为空,并从列表中选择 var 选项,如下面的屏幕截图所示。选择 var 文件夹。 - Step 2: Select lib folder.
步骤 2:选择 lib 文件夹。 - Step 3: Select pgadmin folder. Here you could notice the folders are locked except the pgadmin folder.
步骤 3:选择 pgadmin 文件夹。在这里,您可以注意到除了 pgadmin 文件夹之外,其他文件夹都是锁定的。 - Step 4: Now select upload as mentioned here.
第 4 步:现在按照此处所述选择上传。 - Step 5: Now Drag and drop the file from your downloads on your local machine.
第 5 步:现在将文件从您本地计算机的下载中拖放到此处。 - Step 6: Finally, the upload is successful. When the upload is complete, close the drop files area by clicking X.
第 6 步:最后,上传成功。上传完成后,通过点击 X 关闭文件拖放区域。
- Step 1: Initially make sure the folder details empty and select the var option from the list as shown in the screenshot below. Select var folder
- Select the uploaded myauthors.csv file from the list and click Select.
从列表中选择上传的 myauthors.csv 文件,然后点击选择。 - Ensure the file has selected.
确保文件已选择。 - Under Options enable Header and Click OK and notification of import success will appear.
在选项中启用页眉,然后点击确定,将出现导入成功的通知。
- Make sure Import/Export is set to Import,
- Repeat Task C Step 1 to check that the newly imported data rows appear along with your previously inserted 2 rows.
重复任务 C 步骤 1,检查新导入的数据行是否与您之前插入的 2 行一起出现。
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.
如您所见,csv 文件中的数据已成功上传到表格中,您不必手动输入数百条记录。
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.