Spatial Database Lab1 PostgreSQL Tutorial I GUI
Spatial Database Lab1 PostgreSQL Tutorial I GUI
Please first go through Part I: Exercise Tutorial, then finish tasks listed in Part II: Lab Assignment.
The server should now be shown in the left hand box and you can navigate your way round your database tables. For
the first time using the PostgreSQL, only two database available, including a spatial database (postgis_21_sample)
created while installing the PostGIS, and a default database named postgres on each PostgreSQL server installation.
In this practice, we will add a table to firstDB to record weather data in various
cities across the country. To do this, we need:
Add fields to the table: Click Columns tab, click Add button, and enter name in the Name text box,
Data type as character and Length as 80
Add one more field to the table: click Add button again, and enter location in the Name text box, and Data
type as Point.
Finally, lets add one simple constraint for the table. This would be our primary key, which would prevent
duplicated cities from being add to the table. This can be done by:
Click Constraints tab -> Add button. A new dialogue for adding a new primary key will show up. Enter
the pk_city_name as the key name, click Columns, and connect the key to a city name field of the table we
created by selecting name. Then we will see that our primary key is created and added in the panel.
Now we have specified the table name, column names, their types, and a simple constraint (primary key) to
prevent from duplicated records with the same city being added to the table. To the result of our work, you can
go to the object browser and expand the table cities.
Clicking the SQL icon will produce another window in which the SQL query should be written.
SQL icon
Following figure is a screenshot of the SQL window where the queries are written.
Following are SQL command that we can use to create a weather table:
White space (i.e., spaces, tabs, and newlines) can be used freely in SQL commands. That means you can type the
command aligned differently than above, or even all on one line. Two dashes ("--") introduce comments. Whatever
follows them is ignored up to the end of the line. SQL is case insensitive about key words and identifiers, except
when identifiers are double-quoted to preserve the case (not done above).
varchar(80) specifies a data type that can store arbitrary character strings up to 80 characters in length. int is the
normal integer type. real is a type for storing single precision floating-point numbers. date should be self-
explanatory. (Yes, the column of type date is also named date. This might be convenient or confusing you
choose.)
PostgreSQL supports the standard SQL types int, small int, real, double precision, char(N), varchar(N), date, time,
timestamp, and interval, as well as other types of general utility and a rich set of geometric types. PostgreSQL can
be customized with an arbitrary number of user-defined data types. Consequently, type names are not syntactical
key words, except where required to support special cases in the SQL standard.
Now lets copy the SQL to the SQL editor window. Before you execute the command by click Execute
pgScript button, you need to make sure that you are connecting to the correct database (DB) server and
DB as well. In our case, our DB name is firstDB.
If the connection is not to the firstDB, you can scroll down the DB connection drop box as below and select
new connection. A DB connection dialogue will show up and you can select the appropriate one.
Enter the Madison, (89.40, 43.07) as name and location for one row, and add San Francisco, (-122.43,37.78)
as another row as below (you might need to click the Refresh button after enter each row).
However, this is not efficient way to enter the data. It is most useful for adding simple test data. On the other hand,
we can use SQL query tool again, to enter data in batch. Following is a SQL command to insert three records to the
weather table.
Now lets erase SQL commands in the SQL Editor panel by clicking the clear edit window tool bar.
Copy the SQL to the SQL editor window, and click Execute pgScript button to run the command
If you open the weather table in the Object browser panel, you will see that three records are added.
Now we have the data in multiple tables, we can use query tool to view all our whether data. In todays exercise, we
will learn to use Graphical Query Builder rather than SQL Editor to build the SQL commands to view the data.
The Graphical Query Builder (GQB) is part of the Query Tool which allows you to build simple SQL queries
visually1.
Click SQL query tool to open the query window, and erase SQL commands in the SQL Editor panel by
clicking the clean edit windowtool bar so we can have a clean window. Then click Graphical Query
Builder
Open Schemas-> public. Drag the cities onto the graphic query builder canvas. You will see a pane of
cities showing up in the canvas. Drag the weather table onto the canvas as well. Now you can select the
information you want to view from both tables. Under the cities pane, you can check name, location, and
check temp_lo, temp_hi, and date from the weather table.
1
http://www.pgadmin.org/docs/dev/gqb.html
Now you can do a join between the two tables so we can get the data from both tables by linking the name in
the cities table to the city in the weather table. To create joins between relations, drag a column (name column
in the cities table) from one relation onto another (city column in the weather table).
Now run the query, and we can see down in the output pane, including San Francisco and Madison location and
weather records joining from both table.
Task 1: Insert at least two more records into cities and weather tables. You can make up any city and
weather data. (25 pts)
Task 2: Use Graphical Query Builder to perform a query to list all the cities each with name, location, temp_lo,
temp_hi, prcp, and date. (25 pts)
Directions: explain how you complete the tasks in texts, along with screenshots, and/or SQL code, to indicate
your steps and results if necessary. Please turn in your lab report as a single .pdf file. Submit this file to the
Dropbox at Learn@UW by the beginning of your lab period in one week.
Reference: