SlideShare a Scribd company logo
Introduction to Databases with PostgreSQL
Gabrielle Roth
PDXPUG & FreeGeek
Nov 9, 2010
Outline
Intro to Databases (discussion)
PostgreSQL (discussion)
psql (hands-on)
SQL (hands-on)
...break somewhere in here, 7:30...
Practice db + more SQL (hands-on)
Basic Admin + GUI Tools (discussion)
Questions
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 2 / 1
Introductions
__ __
/ ~~~/  . o O ( Hi! )
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
Thanks to Hayley J Wakenshaw for the Elephant
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 3 / 1
Databases!
A place to keep your data!
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 4 / 1
Relational databases
Based on Relational Calculus
Data is stored in ”relations”
”Normalized”
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 5 / 1
PostgreSQL
- How do you say that?
- It’s the database *server*
- We think it’s the best.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 6 / 1
Get connected.
chmod 600 /path/to/id_pdxpug
ssh -i /path/to/id_pdxpug pdxpug@207.173.203.228
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 7 / 1
Pg command-line interface
- psql
- - -help
- psql -U [username] -d pdxpug
- h
- ?
- other useful commands
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 8 / 1
SQL
- Declarative programming language
- ...let’s do ”Hello World”.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 9 / 1
Hello, World.
SELECT ’Hello, World.’;
...oh yeah, and there’s command-completion, too.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 10 / 1
CREATE TABLE
CREATE TABLE animals
(name varchar(32) primary key,
skin varchar(32),
legs int);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 11 / 1
INSERT
INSERT INTO animals
VALUES (’cat’, ’fur’, ’4’);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 12 / 1
multi-valued INSERT
insert into animals
values
(’dog’, ’fur’, ’4’),
(’bird’, ’feathers’, ’2’),
(’snake’, ’scales’, ’0’);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 13 / 1
SELECT
SELECT * FROM animals;
SELECT name, legs FROM animals;
SELECT * FROM animals
ORDER BY name;
SELECT count(*) FROM animals;
SELECT SUM(legs) FROM animals;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 14 / 1
UPDATE
UPDATE animals
SET name = ’kitty’ WHERE name = ’cat’;
SELECT * FROM animals;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 15 / 1
Transactions
BEGIN;
...your stuff...
...check your stuff with a SELECT...
ROLLBACK or COMMIT as desired
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 16 / 1
DELETE
BEGIN;
DELETE FROM animals WHERE legs=2;
SELECT * FROM animals;
[ROLLBACK or COMMIT]
SELECT * FROM animals;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 17 / 1
Exercises
- add an animal of your choice
- update its name
- delete all animals with four legs
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 18 / 1
MOAR TABLEZ
www.postgresqlguide.com/postgresql-sample-database.aspx
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 19 / 1
ERDs
- Entity-Relationship Diagrams
- Graphical representation of the relations
- ...and how they’re connected (JOINed)
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 20 / 1
The time has come, the walrus said, to talk of many things.
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 21 / 1
Load ’er up!
i /home/pdxpug/create_tables.sql
i /home/pdxpug/populate_tables.sql
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 22 / 1
Working with the sample db
- d
- what are these seq relations?
- d item
- s
- e
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 23 / 1
DISTINCT
SELECT fname, lname FROM customer
SELECT count(lname) FROM customer;
SELECT DISTINCT lname FROM customer;
SELECT count(DISTINCT(lname)) FROM customer;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 24 / 1
- PRIMARY vs FOREIGN keys
- Natural vs Surrogate keys
- JOINs
- pset null ’[null]’
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 25 / 1
JOINs
SELECT * FROM item;
SELECT * FROM stock;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
JOIN stock s ON i.item_id = s.item_id;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
LEFT JOIN stock s ON i.item_id = s.item_id;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
RIGHT JOIN stock s ON i.item_id = s.item_id;Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 26 / 1
...and the dreaded Cartesian JOIN
SELECT * FROM item;
SELECT * FROM stock;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i, stock s;
SELECT i.description, i.cost_price, i.sell_price, s.quantity
FROM item i
CROSS JOIN stock s;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 27 / 1
VIEWs and TEMP TABLEs
CREATE VIEW my_view AS SELECT field FROM table;
vs.
CREATE TEMP TABLE my_temp_table AS SELECT field FROM table;
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 28 / 1
VIEWs and TEMP TABLEs
CREATE VIEW view_in_stock AS
SELECT i.item_id, i.description, s.quantity
FROM item i
LEFT JOIN stock s ON i.item_id=s.item_id;
SELECT * FROM view_in_stock;
try:
pset null ’0’
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 29 / 1
subSELECTs
SELECT date_placed FROM orderinfo
WHERE customer_id IN
(SELECT customer_id FROM customer
WHERE lname = ’Matthew’);
SELECT date_placed FROM orderinfo
WHERE customer_id IN
(SELECT customer_id FROM customer
WHERE (fname, lname) = (’Alex’,’Matthew’));
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 30 / 1
Exercises
1 How many items are currently in stock?
2 In which towns do we have customers?
3 Experiment with RIGHT JOIN with the original sample queries.
4 What are the barcodes for each item?
5 What is the name + total ”our cost” value of each item currently in stock?
6 What is the name + total ”selling cost” value of each item currently in stock?
7 What are the total ”our cost” and ”sell cost” values of all items in stock?
8 How much was shipping on each order?
9 What items were ordered by people with the last name ”Stones”?
10 How much was the total shipping per person?
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 31 / 1
Basic Admin
- initdb
- PGDATA
- postgresql.conf
- pg hba.conf
- stop/start/restart/reload
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 32 / 1
Basic Admin
- SELECT version();
- CREATE ROLE
- backups and upgrades
- pg dump, pg dumpall, pg upgrade
- logs
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 33 / 1
GUI Tools
- pHpPgAdmin
- pgAdminIII
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 34 / 1
Extras
- Natural keys vs Surrogate keys
- indexes
GRANT USAGE ON SCHEMA [schema] TO [otheruser];
GRANT SELECT ON [table] TO [otheruser];
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 35 / 1
Book giveaway!
SELECT (random() * 100);
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 36 / 1
Where to get help
- www.postgresql.org/community/lists/
- #postgresql
- PDXPUG
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 37 / 1
Reading List
www.postgresql.org/docs
Manga Guide to Databases Takahashi, Mana
Database Design for Mere Mortals Hernandez, Michael J
SQL for Smarties Celko, Joe
Introduction to Database Systems Date, CJ
Relational Model for Database Management Codd, EF
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 38 / 1
__ __
/ ~~~/  . o O ( Thank you! )
,----( oo )
/ __ __/
/| ( |(
^  /___ / |
|__| |__|-"
Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 39 / 1

More Related Content

What's hot (20)

InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxData
 
Modeling Data and Queries for Wide Column NoSQL
Modeling Data and Queries for Wide Column NoSQL
ScyllaDB
 
Using ClickHouse for Experimentation
Using ClickHouse for Experimentation
Gleb Kanterov
 
PostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
Introduction to PostgreSQL
Introduction to PostgreSQL
Joel Brewer
 
Oracle RAC Virtualized - In VMs, in Containers, On-premises, and in the Cloud
Oracle RAC Virtualized - In VMs, in Containers, On-premises, and in the Cloud
Markus Michalewicz
 
Oracle Database Introduction
Oracle Database Introduction
Chhom Karath
 
Understanding Query Plans and Spark UIs
Understanding Query Plans and Spark UIs
Databricks
 
A Thorough Comparison of Delta Lake, Iceberg and Hudi
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Databricks
 
Postgresql database administration volume 1
Postgresql database administration volume 1
Federico Campoli
 
Apache Spark Overview
Apache Spark Overview
Vadim Y. Bichutskiy
 
Oracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
Presto best practices for Cluster admins, data engineers and analysts
Presto best practices for Cluster admins, data engineers and analysts
Shubham Tagra
 
Presto anatomy
Presto anatomy
Dongmin Yu
 
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
Databricks
 
Mastering PostgreSQL Administration
Mastering PostgreSQL Administration
EDB
 
Get to know PostgreSQL!
Get to know PostgreSQL!
Oddbjørn Steffensen
 
Presto
Presto
Knoldus Inc.
 
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
PgDay.Seoul
 
[DSC Europe 22] Lakehouse architecture with Delta Lake and Databricks - Draga...
[DSC Europe 22] Lakehouse architecture with Delta Lake and Databricks - Draga...
DataScienceConferenc1
 
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxData
 
Modeling Data and Queries for Wide Column NoSQL
Modeling Data and Queries for Wide Column NoSQL
ScyllaDB
 
Using ClickHouse for Experimentation
Using ClickHouse for Experimentation
Gleb Kanterov
 
PostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
Introduction to PostgreSQL
Introduction to PostgreSQL
Joel Brewer
 
Oracle RAC Virtualized - In VMs, in Containers, On-premises, and in the Cloud
Oracle RAC Virtualized - In VMs, in Containers, On-premises, and in the Cloud
Markus Michalewicz
 
Oracle Database Introduction
Oracle Database Introduction
Chhom Karath
 
Understanding Query Plans and Spark UIs
Understanding Query Plans and Spark UIs
Databricks
 
A Thorough Comparison of Delta Lake, Iceberg and Hudi
A Thorough Comparison of Delta Lake, Iceberg and Hudi
Databricks
 
Postgresql database administration volume 1
Postgresql database administration volume 1
Federico Campoli
 
Oracle RAC 19c: Best Practices and Secret Internals
Oracle RAC 19c: Best Practices and Secret Internals
Anil Nair
 
Presto best practices for Cluster admins, data engineers and analysts
Presto best practices for Cluster admins, data engineers and analysts
Shubham Tagra
 
Presto anatomy
Presto anatomy
Dongmin Yu
 
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
Scaling and Unifying SciKit Learn and Apache Spark Pipelines
Databricks
 
Mastering PostgreSQL Administration
Mastering PostgreSQL Administration
EDB
 
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
PgDay.Seoul
 
[DSC Europe 22] Lakehouse architecture with Delta Lake and Databricks - Draga...
[DSC Europe 22] Lakehouse architecture with Delta Lake and Databricks - Draga...
DataScienceConferenc1
 

Similar to Introduction to PostgreSQL (20)

Object Relational Database Management System
Object Relational Database Management System
Amar Myana
 
Don't panic! - Postgres introduction
Don't panic! - Postgres introduction
Federico Campoli
 
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
MhmdMk10
 
Demystifying PostgreSQL (Zendcon 2010)
Demystifying PostgreSQL (Zendcon 2010)
NOLOH LLC.
 
Demystifying PostgreSQL
Demystifying PostgreSQL
NOLOH LLC.
 
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Citus Data
 
PostgreSQL Terminology
PostgreSQL Terminology
Showmax Engineering
 
PostgreSQL
PostgreSQL
Reuven Lerner
 
The Accidental DBA
The Accidental DBA
PostgreSQL Experts, Inc.
 
0292-introduction-postgresql.pdf
0292-introduction-postgresql.pdf
Mustafa Keskin
 
Introduction to PostgreSQL
Introduction to PostgreSQL
Jim Mlodgenski
 
PostgreSQL 9.0 & The Future
PostgreSQL 9.0 & The Future
Aaron Thul
 
CSC439-3.pdf
CSC439-3.pdf
muazumuhammad6
 
Really Big Elephants: PostgreSQL DW
Really Big Elephants: PostgreSQL DW
PostgreSQL Experts, Inc.
 
Migrating To PostgreSQL
Migrating To PostgreSQL
Grant Fritchey
 
Postgresql Database Administration Basic - Day2
Postgresql Database Administration Basic - Day2
PoguttuezhiniVP
 
Lecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erick
okelloerick
 
Manipulating Data in Style with SQL
Manipulating Data in Style with SQL
Ryan B Harvey, CSDP, CSM
 
PostgreSQL : Introduction
PostgreSQL : Introduction
Open Source School
 
An evening with Postgresql
An evening with Postgresql
Joshua Drake
 
Object Relational Database Management System
Object Relational Database Management System
Amar Myana
 
Don't panic! - Postgres introduction
Don't panic! - Postgres introduction
Federico Campoli
 
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
Chjkkkkkkkkkkkkkkkkkjjjjjjjjjjjjjjjjjjjjjjjjjj01_The Basics.pptx
MhmdMk10
 
Demystifying PostgreSQL (Zendcon 2010)
Demystifying PostgreSQL (Zendcon 2010)
NOLOH LLC.
 
Demystifying PostgreSQL
Demystifying PostgreSQL
NOLOH LLC.
 
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Whats wrong with postgres | PGConf EU 2019 | Craig Kerstiens
Citus Data
 
0292-introduction-postgresql.pdf
0292-introduction-postgresql.pdf
Mustafa Keskin
 
Introduction to PostgreSQL
Introduction to PostgreSQL
Jim Mlodgenski
 
PostgreSQL 9.0 & The Future
PostgreSQL 9.0 & The Future
Aaron Thul
 
Migrating To PostgreSQL
Migrating To PostgreSQL
Grant Fritchey
 
Postgresql Database Administration Basic - Day2
Postgresql Database Administration Basic - Day2
PoguttuezhiniVP
 
Lecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erick
okelloerick
 
An evening with Postgresql
An evening with Postgresql
Joshua Drake
 
Ad

More from Mark Wong (20)

OHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
Mark Wong
 
OHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 Report
Mark Wong
 
collectd & PostgreSQL
collectd & PostgreSQL
Mark Wong
 
Android & PostgreSQL
Android & PostgreSQL
Mark Wong
 
PGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this app
Mark Wong
 
Developing PGTop for Android
Developing PGTop for Android
Mark Wong
 
Pg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentation
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
Filesystem Performance from a Database Perspective
Filesystem Performance from a Database Perspective
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
Mark Wong
 
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
Mark Wong
 
Linux Filesystems, RAID, and more
Linux Filesystems, RAID, and more
Mark Wong
 
pg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQL
Mark Wong
 
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
Mark Wong
 
OHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 Report
Mark Wong
 
collectd & PostgreSQL
collectd & PostgreSQL
Mark Wong
 
Android & PostgreSQL
Android & PostgreSQL
Mark Wong
 
PGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this app
Mark Wong
 
Developing PGTop for Android
Developing PGTop for Android
Mark Wong
 
Pg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentation
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
Mark Wong
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
Mark Wong
 
Filesystem Performance from a Database Perspective
Filesystem Performance from a Database Perspective
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
Mark Wong
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
Mark Wong
 
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
Mark Wong
 
Linux Filesystems, RAID, and more
Linux Filesystems, RAID, and more
Mark Wong
 
pg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQL
Mark Wong
 
Ad

Introduction to PostgreSQL

  • 1. Introduction to Databases with PostgreSQL Gabrielle Roth PDXPUG & FreeGeek Nov 9, 2010
  • 2. Outline Intro to Databases (discussion) PostgreSQL (discussion) psql (hands-on) SQL (hands-on) ...break somewhere in here, 7:30... Practice db + more SQL (hands-on) Basic Admin + GUI Tools (discussion) Questions Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 2 / 1
  • 3. Introductions __ __ / ~~~/ . o O ( Hi! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" Thanks to Hayley J Wakenshaw for the Elephant Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 3 / 1
  • 4. Databases! A place to keep your data! Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 4 / 1
  • 5. Relational databases Based on Relational Calculus Data is stored in ”relations” ”Normalized” Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 5 / 1
  • 6. PostgreSQL - How do you say that? - It’s the database *server* - We think it’s the best. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 6 / 1
  • 7. Get connected. chmod 600 /path/to/id_pdxpug ssh -i /path/to/id_pdxpug [email protected] Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 7 / 1
  • 8. Pg command-line interface - psql - - -help - psql -U [username] -d pdxpug - h - ? - other useful commands Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 8 / 1
  • 9. SQL - Declarative programming language - ...let’s do ”Hello World”. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 9 / 1
  • 10. Hello, World. SELECT ’Hello, World.’; ...oh yeah, and there’s command-completion, too. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 10 / 1
  • 11. CREATE TABLE CREATE TABLE animals (name varchar(32) primary key, skin varchar(32), legs int); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 11 / 1
  • 12. INSERT INSERT INTO animals VALUES (’cat’, ’fur’, ’4’); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 12 / 1
  • 13. multi-valued INSERT insert into animals values (’dog’, ’fur’, ’4’), (’bird’, ’feathers’, ’2’), (’snake’, ’scales’, ’0’); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 13 / 1
  • 14. SELECT SELECT * FROM animals; SELECT name, legs FROM animals; SELECT * FROM animals ORDER BY name; SELECT count(*) FROM animals; SELECT SUM(legs) FROM animals; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 14 / 1
  • 15. UPDATE UPDATE animals SET name = ’kitty’ WHERE name = ’cat’; SELECT * FROM animals; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 15 / 1
  • 16. Transactions BEGIN; ...your stuff... ...check your stuff with a SELECT... ROLLBACK or COMMIT as desired Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 16 / 1
  • 17. DELETE BEGIN; DELETE FROM animals WHERE legs=2; SELECT * FROM animals; [ROLLBACK or COMMIT] SELECT * FROM animals; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 17 / 1
  • 18. Exercises - add an animal of your choice - update its name - delete all animals with four legs Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 18 / 1
  • 19. MOAR TABLEZ www.postgresqlguide.com/postgresql-sample-database.aspx Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 19 / 1
  • 20. ERDs - Entity-Relationship Diagrams - Graphical representation of the relations - ...and how they’re connected (JOINed) Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 20 / 1
  • 21. The time has come, the walrus said, to talk of many things. Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 21 / 1
  • 22. Load ’er up! i /home/pdxpug/create_tables.sql i /home/pdxpug/populate_tables.sql Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 22 / 1
  • 23. Working with the sample db - d - what are these seq relations? - d item - s - e Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 23 / 1
  • 24. DISTINCT SELECT fname, lname FROM customer SELECT count(lname) FROM customer; SELECT DISTINCT lname FROM customer; SELECT count(DISTINCT(lname)) FROM customer; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 24 / 1
  • 25. - PRIMARY vs FOREIGN keys - Natural vs Surrogate keys - JOINs - pset null ’[null]’ Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 25 / 1
  • 26. JOINs SELECT * FROM item; SELECT * FROM stock; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i JOIN stock s ON i.item_id = s.item_id; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i LEFT JOIN stock s ON i.item_id = s.item_id; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i RIGHT JOIN stock s ON i.item_id = s.item_id;Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 26 / 1
  • 27. ...and the dreaded Cartesian JOIN SELECT * FROM item; SELECT * FROM stock; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i, stock s; SELECT i.description, i.cost_price, i.sell_price, s.quantity FROM item i CROSS JOIN stock s; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 27 / 1
  • 28. VIEWs and TEMP TABLEs CREATE VIEW my_view AS SELECT field FROM table; vs. CREATE TEMP TABLE my_temp_table AS SELECT field FROM table; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 28 / 1
  • 29. VIEWs and TEMP TABLEs CREATE VIEW view_in_stock AS SELECT i.item_id, i.description, s.quantity FROM item i LEFT JOIN stock s ON i.item_id=s.item_id; SELECT * FROM view_in_stock; try: pset null ’0’ Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 29 / 1
  • 30. subSELECTs SELECT date_placed FROM orderinfo WHERE customer_id IN (SELECT customer_id FROM customer WHERE lname = ’Matthew’); SELECT date_placed FROM orderinfo WHERE customer_id IN (SELECT customer_id FROM customer WHERE (fname, lname) = (’Alex’,’Matthew’)); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 30 / 1
  • 31. Exercises 1 How many items are currently in stock? 2 In which towns do we have customers? 3 Experiment with RIGHT JOIN with the original sample queries. 4 What are the barcodes for each item? 5 What is the name + total ”our cost” value of each item currently in stock? 6 What is the name + total ”selling cost” value of each item currently in stock? 7 What are the total ”our cost” and ”sell cost” values of all items in stock? 8 How much was shipping on each order? 9 What items were ordered by people with the last name ”Stones”? 10 How much was the total shipping per person? Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 31 / 1
  • 32. Basic Admin - initdb - PGDATA - postgresql.conf - pg hba.conf - stop/start/restart/reload Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 32 / 1
  • 33. Basic Admin - SELECT version(); - CREATE ROLE - backups and upgrades - pg dump, pg dumpall, pg upgrade - logs Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 33 / 1
  • 34. GUI Tools - pHpPgAdmin - pgAdminIII Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 34 / 1
  • 35. Extras - Natural keys vs Surrogate keys - indexes GRANT USAGE ON SCHEMA [schema] TO [otheruser]; GRANT SELECT ON [table] TO [otheruser]; Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 35 / 1
  • 36. Book giveaway! SELECT (random() * 100); Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 36 / 1
  • 37. Where to get help - www.postgresql.org/community/lists/ - #postgresql - PDXPUG Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 37 / 1
  • 38. Reading List www.postgresql.org/docs Manga Guide to Databases Takahashi, Mana Database Design for Mere Mortals Hernandez, Michael J SQL for Smarties Celko, Joe Introduction to Database Systems Date, CJ Relational Model for Database Management Codd, EF Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 38 / 1
  • 39. __ __ / ~~~/ . o O ( Thank you! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" Gabrielle Roth (PDXPUG & FreeGeek) Introduction to Databases with PostgreSQL Nov 9, 2010 39 / 1