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

PostgreSQL
PostgreSQL PostgreSQL
PostgreSQL
Amazon Web Services
 
Auditing and Monitoring PostgreSQL/EPAS
Auditing and Monitoring PostgreSQL/EPASAuditing and Monitoring PostgreSQL/EPAS
Auditing and Monitoring PostgreSQL/EPAS
EDB
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
Jim Mlodgenski
 
Your first ClickHouse data warehouse
Your first ClickHouse data warehouseYour first ClickHouse data warehouse
Your first ClickHouse data warehouse
Altinity Ltd
 
Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...
Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...
Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...
Carlos Sierra
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
Reuven Lerner
 
An Overview of Apache Cassandra
An Overview of Apache CassandraAn Overview of Apache Cassandra
An Overview of Apache Cassandra
DataStax
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Zohar Elkayam
 
Migration From Oracle to PostgreSQL
Migration From Oracle to PostgreSQLMigration From Oracle to PostgreSQL
Migration From Oracle to PostgreSQL
PGConf APAC
 
Migrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQLMigrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Jim Mlodgenski
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
valuebound
 
Practical Partitioning in Production with Postgres
Practical Partitioning in Production with PostgresPractical Partitioning in Production with Postgres
Practical Partitioning in Production with Postgres
EDB
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
EXEM
 
Migrating Oracle to PostgreSQL
Migrating Oracle to PostgreSQLMigrating Oracle to PostgreSQL
Migrating Oracle to PostgreSQL
Amazon Web Services
 
Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21
Stamatis Zampetakis
 
SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?
Brent Ozar
 
[pgday.Seoul 2022] PostgreSQL with Google Cloud
[pgday.Seoul 2022] PostgreSQL with Google Cloud[pgday.Seoul 2022] PostgreSQL with Google Cloud
[pgday.Seoul 2022] PostgreSQL with Google Cloud
PgDay.Seoul
 
Understanding PostgreSQL LW Locks
Understanding PostgreSQL LW LocksUnderstanding PostgreSQL LW Locks
Understanding PostgreSQL LW Locks
Jignesh Shah
 
ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...
ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...
ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...
Altinity Ltd
 

What's hot (20)

PostgreSQL
PostgreSQL PostgreSQL
PostgreSQL
 
Auditing and Monitoring PostgreSQL/EPAS
Auditing and Monitoring PostgreSQL/EPASAuditing and Monitoring PostgreSQL/EPAS
Auditing and Monitoring PostgreSQL/EPAS
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
Your first ClickHouse data warehouse
Your first ClickHouse data warehouseYour first ClickHouse data warehouse
Your first ClickHouse data warehouse
 
Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...
Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...
Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Opt...
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
An Overview of Apache Cassandra
An Overview of Apache CassandraAn Overview of Apache Cassandra
An Overview of Apache Cassandra
 
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAsOracle Database Performance Tuning Advanced Features and Best Practices for DBAs
Oracle Database Performance Tuning Advanced Features and Best Practices for DBAs
 
Migration From Oracle to PostgreSQL
Migration From Oracle to PostgreSQLMigration From Oracle to PostgreSQL
Migration From Oracle to PostgreSQL
 
Migrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQLMigrating Oracle database to PostgreSQL
Migrating Oracle database to PostgreSQL
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
 
Practical Partitioning in Production with Postgres
Practical Partitioning in Production with PostgresPractical Partitioning in Production with Postgres
Practical Partitioning in Production with Postgres
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
 
Migrating Oracle to PostgreSQL
Migrating Oracle to PostgreSQLMigrating Oracle to PostgreSQL
Migrating Oracle to PostgreSQL
 
Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21
 
SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?
 
[pgday.Seoul 2022] PostgreSQL with Google Cloud
[pgday.Seoul 2022] PostgreSQL with Google Cloud[pgday.Seoul 2022] PostgreSQL with Google Cloud
[pgday.Seoul 2022] PostgreSQL with Google Cloud
 
Understanding PostgreSQL LW Locks
Understanding PostgreSQL LW LocksUnderstanding PostgreSQL LW Locks
Understanding PostgreSQL LW Locks
 
ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...
ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...
ClickHouse Data Warehouse 101: The First Billion Rows, by Alexander Zaitsev a...
 

Similar to Introduction to PostgreSQL

Pig workshop
Pig workshopPig workshop
Pig workshop
Sudar Muthu
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
Jeffrey Kemp
 
Pg big fast ugly acid
Pg big fast ugly acidPg big fast ugly acid
Pg big fast ugly acid
Federico Campoli
 
Don't panic! - Postgres introduction
Don't panic! - Postgres introductionDon't panic! - Postgres introduction
Don't panic! - Postgres introduction
Federico Campoli
 
Massively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian HustonMassively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian Huston
PyData
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
arihantmobileselepun
 
Hitchikers guide handout
Hitchikers guide handoutHitchikers guide handout
Hitchikers guide handout
Federico Campoli
 
Hadoop
HadoopHadoop
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
Vu Hung Nguyen
 
Kill the DBA
Kill the DBAKill the DBA
Kill the DBA
Knut Haugen
 
Rails in the enterprise
Rails in the enterpriseRails in the enterprise
Rails in the enterprise
alexrothenberg
 
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Sumeet Singh
 
Getting Started with Hadoop
Getting Started with HadoopGetting Started with Hadoop
Getting Started with Hadoop
Josh Devins
 
Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.
Dan Lynn
 
Data-Intensive Scalable Science
Data-Intensive Scalable ScienceData-Intensive Scalable Science
Data-Intensive Scalable Science
University of Washington
 
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Robert Metzger
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
Jane Chung
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
Oursky
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
ajayadinathcomputers
 
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Julian Hyde
 

Similar to Introduction to PostgreSQL (20)

Pig workshop
Pig workshopPig workshop
Pig workshop
 
Old Oracle Versions
Old Oracle VersionsOld Oracle Versions
Old Oracle Versions
 
Pg big fast ugly acid
Pg big fast ugly acidPg big fast ugly acid
Pg big fast ugly acid
 
Don't panic! - Postgres introduction
Don't panic! - Postgres introductionDon't panic! - Postgres introduction
Don't panic! - Postgres introduction
 
Massively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian HustonMassively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian Huston
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
Hitchikers guide handout
Hitchikers guide handoutHitchikers guide handout
Hitchikers guide handout
 
Hadoop
HadoopHadoop
Hadoop
 
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
 
Kill the DBA
Kill the DBAKill the DBA
Kill the DBA
 
Rails in the enterprise
Rails in the enterpriseRails in the enterprise
Rails in the enterprise
 
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
Strata Conference + Hadoop World San Jose 2015: Data Discovery on Hadoop
 
Getting Started with Hadoop
Getting Started with HadoopGetting Started with Hadoop
Getting Started with Hadoop
 
Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.
 
Data-Intensive Scalable Science
Data-Intensive Scalable ScienceData-Intensive Scalable Science
Data-Intensive Scalable Science
 
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
Stratosphere System Overview Big Data Beers Berlin. 20.11.2013
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
 
Tree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdfTree Traversals A tree traversal is the process of visiting.pdf
Tree Traversals A tree traversal is the process of visiting.pdf
 
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
Planning with Polyalgebra: Bringing Together Relational, Complex and Machine ...
 

More from Mark Wong

OHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 MockumentaryOHAI, 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 ReportOHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 Report
Mark Wong
 
collectd & PostgreSQL
collectd & PostgreSQLcollectd & PostgreSQL
collectd & PostgreSQL
Mark Wong
 
Android & PostgreSQL
Android & PostgreSQLAndroid & PostgreSQL
Android & PostgreSQL
Mark Wong
 
PGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this appPGTop 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 AndroidDeveloping PGTop for Android
Developing PGTop for Android
Mark Wong
 
Pg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentationPg 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 PostgreSQLpg_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 PostgreSQLpg_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 TuningPostgreSQL 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 PostgreSQLpg_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 PerspectiveFilesystem 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...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 HowtoPostgreSQL 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...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 BackgroundPostgreSQL 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...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_proctabpg_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 moreLinux Filesystems, RAID, and more
Linux Filesystems, RAID, and more
Mark Wong
 
pg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQLpg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQL
Mark Wong
 

More from Mark Wong (20)

OHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 MockumentaryOHAI, my name is Chelnik! PGCon 2014 Mockumentary
OHAI, my name is Chelnik! PGCon 2014 Mockumentary
 
OHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 ReportOHAI, my name is Chelnik! Postgres Open 2013 Report
OHAI, my name is Chelnik! Postgres Open 2013 Report
 
collectd & PostgreSQL
collectd & PostgreSQLcollectd & PostgreSQL
collectd & PostgreSQL
 
Android & PostgreSQL
Android & PostgreSQLAndroid & PostgreSQL
Android & PostgreSQL
 
PGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this appPGTop for Android: Things I learned making this app
PGTop for Android: Things I learned making this app
 
Developing PGTop for Android
Developing PGTop for AndroidDeveloping PGTop for Android
Developing PGTop for Android
 
Pg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentationPg in-the-brazilian-armed-forces-presentation
Pg in-the-brazilian-armed-forces-presentation
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
PostgreSQL Portland Performance Practice Project - Database Test 2 TuningPostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
PostgreSQL Portland Performance Practice Project - Database Test 2 Tuning
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
Filesystem Performance from a Database Perspective
Filesystem Performance from a Database PerspectiveFilesystem Performance from a Database Perspective
Filesystem Performance from a Database Perspective
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
PostgreSQL Portland Performance Practice Project - Database Test 2 Filesystem...
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
PostgreSQL Portland Performance Practice Project - Database Test 2 HowtoPostgreSQL Portland Performance Practice Project - Database Test 2 Howto
PostgreSQL Portland Performance Practice Project - Database Test 2 Howto
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
PostgreSQL Portland Performance Practice Project - Database Test 2 Workload D...
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
PostgreSQL Portland Performance Practice Project - Database Test 2 BackgroundPostgreSQL Portland Performance Practice Project - Database Test 2 Background
PostgreSQL Portland Performance Practice Project - Database Test 2 Background
 
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
PostgreSQL Portland Performance Practice Project - Database Test 2 Series Ove...
 
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
pg_top is 'top' for PostgreSQL: pg_top + pg_proctabpg_top is 'top' for PostgreSQL: pg_top + pg_proctab
pg_top is 'top' for PostgreSQL: pg_top + pg_proctab
 
Linux Filesystems, RAID, and more
Linux Filesystems, RAID, and moreLinux Filesystems, RAID, and more
Linux Filesystems, RAID, and more
 
pg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQLpg_top is 'top' for PostgreSQL
pg_top is 'top' for PostgreSQL
 

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 pdxpug@207.173.203.228 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