Intro To PostgreSQL
Intro To PostgreSQL
Intro To PostgreSQL
Mike Callahan
WFO Louisville
September 8, 2009
Topics
Relational database theory
PostgreSQL Reference
psql
Snoopy
SQL syntax
SELECT
UPDATE
INSERT INTO
DELETE FROM
CREATE TEMP TABLE
COPY
Databases in AWIPS
Data
Sources PostgreSQL
SQL
D2D
WHFS
psql
pg_...
Databases Other
Applications
Relational Theory
Data are stored in groups called 'tables' or 'relations'
Tables consist of 'rows' or 'records' and 'columns' or
'fields'
Rows are usually identified by a unique 'key' which
may be a single column or a group of columns
Columns can be linked to other tables with similar
columns
Good design of the database structure or 'schema' is
critical.
"All the data in a row should be dependant on the key, the
whole key, and nothing but the key."
Working with databases
Most of the time, you will work with existing
databases
However, you still need to know the schema
We will concentrate on four tasks
Extracting data
Changing data
Inserting data
Deleting data
Why PostgreSQL
Switching database server from HP to Linux
PostgreSQL is free, Informix for Linux is not
PostgreSQL is the most advanced open source
relational database, MySQL did not have
triggers, PostgreSQL did
Version on AWIPS is 8.2, latest version is 8.4
Documentation is excellent and is at
www.postgresql.org/docs/8.2/...
We will use version 8.2 for our examples
PostgreSQL Documentation
Preface
Gives history, conventions, etc.
Tutorial
A quick introduction to SQL
SQL Language
A description of the standard and the PostgreSQL
additions
Reference: SQL Commands
Details of each command
psql
…/interactive/app-psql.html
The standard PostgreSQL application to work
with databases
It uses a superset of standard SQL
It can be interactively like DBaccess, or in
scripts like sqlcmd
Common psql Command Line Options
-A, --no-align: set output to non-aligned, no padding
-c sql, --command sql: execute the sql command and
then exit
-d name, --dbname name: name of database, same as
name as the first non-option argument
-f name, --file name: use name as the source of
commands
-o name, --output name: put the output in name
-q, --quiet: suppress welcome messages
-t, --tuples-only: suppress print column names, result
row counters, etc
-?, --help: get command line help
Common psql Meta-commands
\a: toggle output format (aligned/unaligned)
\c name: connect to a database name
\copy table: copy a table to/from a file
\d : list all tables (display)
\d table: show information about a table
\e: edit the query buffer
\g: execute the query buffer (go)
\h command: display help on command
\i name: read name into query buffer (input)
Common psql Meta-commands
\o name: send output to name
\p: display the query buffer
\q: quit the program
\r: resets (clears) the query buffer
\t: toggle the output headers on/off
\w name: write the query buffer to name
\! command: execute the Linux command
\?: help on meta-commands
Interactive psql
Typing in a query loads the query buffer
The command will not run unless terminated
with a semicolon (;)
Meta-commands all start with blackslash (\)
Exit with \q
Snoopy
Snoopy is an interactive, GUI front-end to psql
It was designed for the hydro database, but
will work with any AWIPS database
Queries are in a scrolling window, data is in a
separate scrolling window
Help is available about tables, columns, rows,
SQL syntax, and hydro database data
dictionary
Available on the AWIPS LAD
Snoopy Screen Shot
Snoopy Help
List of List of
Condition Expression
columns tables
_ - a single character
WHERE lid LIKE '___K1' or lid LIKE '___K2'
~
Used to set up POSIX regular expression searches
WHERE lid ~ '…K[12]'
Use escape notation for backslash expressions
'…\w\d' becomes e'…\\w\\d'
Substrings
Use to extract part of a character column based
on position (count from 1)
SUBSTRING(column FROM begin FOR length)
Ex. show phone number of observers in the
Kansas City KS Metro area code
SELECT name, hphone FROM observer WHERE
SUBSTRING(hphone FROM 1 FOR 3) = '913'
SELECT tricks
Testing an expression
SELECT SUBSTRING('ABCDE' FROM 1 FOR 3)
Get all the fields in a table
SELECT * FROM location
Name shortcut
SELECT l.lid, lastname FROM location l, observer o WHERE
l.lid=o.lid AND lastname LIKE 'a%'
Rename columns
SELECT lid AS Location_ID FROM location...
Eliminate duplicate rows
SELECT DISTINCT county FROM location
Order shortcut
SELECT lid, name FROM location ORDER BY 2
Descending order
...ORDER BY lid DESC
Date/Time/Timestamp Functions
AGE(timestamp, [timestamp])
calculate interval between two timestamps, second
argument defaults to NOW
…WHERE AGE(obstime) < INTERVAL '6 hours'
EXTRACT(unit from timestamp)
extract the unit from a timestamp
common units: century, day, decade, dow (day of week
Sun=0, Mon=1, etc), doy (Julian date), hour, minute,
month, quarter, second, timezone, week, year
…WHERE EXTRACT(dow from datcrst) = 3
Group Functions
COUNT()
Instead of displaying the rows, just count them
SELECT COUNT(*) FROM observer
AVG()
Calculate the average of a column
MAX()
Calculate the maximum in a column
MIN()
Calculate the minimum in a column
SUM()
Calculate the sum of a column
The GROUP BY Clause
When using the previous functions, the
GROUP BY clause tells what to group
We want the highest crest at EVERY location,
sorted by ID
If we don't use GROUP BY we will get the
highest crest of ALL IDs instead of the highest
crest at EVERY ID.
SELECT l.lid, MAX(stage) FROM location l, crest c
WHERE l.lid = c.lid GROUP BY l.lid ORDER BY l.lid;
The UPDATE Command
Used to quickly change the values in a column
This command can save a lot of work but can
also be dangerous
Has SET, FROM, and WHERE clauses
Here is a trick:
Create the WHERE clause but use SELECT *
You will see the rows you are going to change
Column List of
Table Condition
and value tables
Using UPDATE
Ex. 913 area code changes to 914
Table and column
observer: hphone
Value
‘914’ || SUBSTRING(hphone FROM 4)
Condition
SUBSTRING(hphone FOR 3) = '913'
Check WHERE Clause
SELECT * FROM observer WHERE SUBSTRING(hphone FOR
3) = '913'
UPDATE rows
UPDATE observer SET hphone = '914‘ || SUBSTRING(hphone FROM
4)
WHERE SUBSTRING(hphone FOR 3) = '913'
The INSERT INTO Command
Insert data into a new row
Has only VALUES clause
Can also use a SELECT query
The INESRT Clause
VALUES
A list of data that you want to insert
If the order of the columns is not listed, it must
match the order defined in the schema
For safety sake, always list the columns
SELECT
A standard select query, but the schema of the
result must match the target
INSERT Command Structure
INSERT INTO VALUES
Table, List of
column list values
-or-
Table, Select
column list query
Using INSERT
Want to add a new row in datum
datum schema is
lid - location id
ddate - datum date
elev - elevation
Table Condition
Using DELETE
Want to remove an observer to make a location
inactive
The location ID is CANK1
Check WHERE Clause
SELECT * FROM observer WHERE lid = 'CANK1'
DELETE row
DELETE FROM observer WHERE lid = 'CANK1'
The CREATE TEMP TABLE
Command
Creates a temporary table that can be used in
future queries
Table is automatically deleted once psql
session is completed.
In Snoopy, table is deleted at end of last
statement in query.
It has a simple structure
CREATE TEMP TABLE Command
Structure
CREATE TEMP TABLE AS
Select
Table
query
Using CREATE TEMP TABLE
Want to copy gage readings for the last three days into a temp
table, junk