• No results found

Getting started with PostgreSQL

N/A
N/A
Protected

Academic year: 2021

Share "Getting started with PostgreSQL"

Copied!
56
0
0

Loading.... (view fulltext now)

Full text

(1)

Getting started with PostgreSQL

Gavin Sherry

[email protected]

Alcove Systems Engineering

(2)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Outline

1

SELECT ‘Hello World’

2

Hiccups

3

Lets do something

4

Some boring stuff

5

Useful stuff

(3)

Installing PostgreSQL and start

Just do it through your package manager!

Fedora/RHEL/CentOS

# yum install postgresql # service postgresql start

Debian/etc

# apt-get search postgresql

# /etc/init.d/postgresql-<version> start

Windows installer

Just run the.msi

http://www.postgresql.org/download/

Source

# ./configure ; make ; make install # initdb /path/to/data

# pg_ctl -D /path/to/data start

(4)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

So, lets go

Example

$ psql postgres

Welcome to psql 8.2.1, the PostgreSQL interactive terminal. Type: \copyright for distribution terms

\h for help with SQL commands \? for help with psql commands

\g or terminate with semicolon to execute query \q to quit

postgres=# \d No relations found.

postgres=# create table foo (i int, t text); CREATE TABLE

postgres=# insert into foo values(1, ’Hello World’); INSERT 0 1

postgres=# select * from foo; i | t

---+---1 | Hello World (1 row)

(5)

Outline

1

SELECT ‘Hello World’

2

Hiccups

3

Lets do something

4

Some boring stuff

(6)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

FATAL...?

What’s this?

psql: FATAL: IDENT authentication failed for user ”gavin”

Some packages tighten up default security

What to do:

su - postgres

createuser gavin

Alternatively, you could relax the authentication – but I wont tell

you how!

(7)

Exploring

Example

# select version();

version

---PostgreSQL 8.2.1 ...

# select table_catalog, table_name, table_type from

information_schema.tables where

table_schema = ’public’;

table_catalog | table_name | table_type

---+---+---postgres

| foo

| BASE TABLE

(8)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

More exploring

Example

# select column_name, data_type from

information_schema.columns

where table_schema=’public’ and

table_name = ’foo’;

column_name | data_type

---+---i

| integer

t

| text

(2 rows)

(9)

Outline

1

SELECT ‘Hello World’

2

Hiccups

3

Lets do something

4

Some boring stuff

(10)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

A little project

Say you want to develop a blog – “everyone’s doing it”

You want to:

1

Store articles

2

Relate articles to blog users

3

Keep statistics on blog posts

(11)

A little project

Say you want to develop a blog – “everyone’s doing it”

You want to:

1

Store articles

2

Relate articles to blog users

3

Keep statistics on blog posts

(12)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

A little project

Say you want to develop a blog – “everyone’s doing it”

You want to:

1

Store articles

2

Relate articles to blog users

3

Keep statistics on blog posts

(13)

A little project

Say you want to develop a blog – “everyone’s doing it”

You want to:

1

Store articles

2

Relate articles to blog users

3

Keep statistics on blog posts

(14)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

blogdb – version 1

SQL

-- Create a table of users (this is a comment, btw) CREATE TABLE users (usrid int, username text, email text); -- insert a user (SQL isn’t case sensitive)

insert into users values(1, ’Gavin’, ’[email protected]’);

-- create the articles table

CREATE TABLE articles (artid int, title text, body text, dt timestamp, usrid int);

-- add a post

insert into articles values(1, ’First post’, ’Yay’, current_timestamp, 1);

-- get article count

SELECT count(*) FROM articles;

(15)

blogdb – version 1

SQL

-- Create a table of users (this is a comment, btw) CREATE TABLE users (usrid int, username text, email text); -- insert a user (SQL isn’t case sensitive)

insert into users values(1, ’Gavin’, ’[email protected]’); -- create the articles table

CREATE TABLE articles (artid int, title text, body text, dt timestamp, usrid int);

-- add a post

insert into articles values(1, ’First post’, ’Yay’, current_timestamp, 1);

-- get article count

(16)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

blogdb – version 1

SQL

-- Create a table of users (this is a comment, btw) CREATE TABLE users (usrid int, username text, email text); -- insert a user (SQL isn’t case sensitive)

insert into users values(1, ’Gavin’, ’[email protected]’); -- create the articles table

CREATE TABLE articles (artid int, title text, body text, dt timestamp, usrid int);

-- add a post

insert into articles values(1, ’First post’, ’Yay’, current_timestamp, 1);

-- get article count

SELECT count(*) FROM articles;

(17)

blogdb – version 1

SQL

-- Create a table of users (this is a comment, btw) CREATE TABLE users (usrid int, username text, email text); -- insert a user (SQL isn’t case sensitive)

insert into users values(1, ’Gavin’, ’[email protected]’); -- create the articles table

CREATE TABLE articles (artid int, title text, body text, dt timestamp, usrid int);

-- add a post

insert into articles values(1, ’First post’, ’Yay’, current_timestamp, 1);

-- get article count

(18)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

blogdb – version 1

SQL

-- Create a table of users (this is a comment, btw) CREATE TABLE users (usrid int, username text, email text); -- insert a user (SQL isn’t case sensitive)

insert into users values(1, ’Gavin’, ’[email protected]’); -- create the articles table

CREATE TABLE articles (artid int, title text, body text, dt timestamp, usrid int);

-- add a post

insert into articles values(1, ’First post’, ’Yay’, current_timestamp, 1);

-- get article count

SELECT count(*) FROM articles;

(19)

What’s wrong with version 1?

No data integrity

No permissions

users

is a common table name – what about other applications

with a

users

table?

(20)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

What’s wrong with version 1?

No data integrity

No permissions

users

is a common table name – what about other applications

with a

users

table?

(21)

What’s wrong with version 1?

No data integrity

No permissions

users

is a common table name – what about other applications

with a

users

table?

(22)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

What’s wrong with version 1?

No data integrity

No permissions

users

is a common table name – what about other applications

with a

users

table?

(23)

What’s wrong with version 1?

No data integrity

No permissions

users

is a common table name – what about other applications

(24)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Data integrity

There are levels of integrity

Referential integrity

Enforce a relationship

Guard against duplication

Each row must have a key

Some data must look a certain way – like dates and times

Some data cannot be ‘undefined’ or

NULL

(25)

blogdb – version 2

SQL

CREATE TABLE users (usrid serial primary key,

username text NOT NULL,

email text NOT NULL,

unique(username));

INSERT INTO users (username, email) values(1, ’Gavin’,

[email protected]’);

CREATE TABLE articles (artid serial primary key,

title text NOT NULL,

body text NOT NULL,

dt timestamp default current_timestamp,

usrid int references users(usrid));

(26)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

blogdb – version 2

SQL

CREATE TABLE users (usrid serial primary key,

username text NOT NULL,

email text NOT NULL,

unique(username));

INSERT INTO users (username, email) values(1, ’Gavin’,

[email protected]’);

CREATE TABLE articles (artid serial primary key,

title text NOT NULL,

body text NOT NULL,

dt timestamp default current_timestamp,

usrid int references users(usrid));

(27)

blogdb – version 2

SQL

CREATE TABLE users (usrid serial primary key,

username text NOT NULL,

email text NOT NULL,

unique(username));

INSERT INTO users (username, email) values(1, ’Gavin’,

[email protected]’);

CREATE TABLE articles (artid serial primary key,

title text NOT NULL,

body text NOT NULL,

dt timestamp default current_timestamp,

usrid int references users(usrid));

(28)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Permissions and privileges

We speak of

CREATE ROLE

and

GRANT

SQL

CREATE ROLE blogread LOGIN;

CREATE ROLE blogwrite LOGIN;

...

GRANT SELECT ON articles,users

TO blogread;

GRANT ALL ON articles,users

TO blobwrite;

(29)

Name space issues

This is why SQL supports “schemas”

SQL

CREATE SCHEMA blog;

SET search_path = blog;

CREATE ...

(30)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Further exercises

Data integrity for email addresses – See

CREATE DOMAIN

Speed up

count(*)

in the presence of a large

articles

table –

see the advanced lecture today

(31)

Outline

1

SELECT ‘Hello World’

2

Hiccups

3

Lets do something

4

Some boring stuff

(32)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

More on PostgreSQL

What’s with the name?

Post (in)

gres, with SQL support

Hey, it used to be named

POSTQUEL

Then again, it used to have a hell

of a lot of Lisp in it

Academic project in the 80s, one of

Michael Stonebraker’s projects at

Berkeley

BSD license – do what you want

with the source (don’t sue us!)

Now developed by a group of

enthusiasts

(33)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(34)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views,

Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(35)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes,

Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(36)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views,

Indexes, Schemas,

Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(37)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes,

Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(38)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas,

Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(39)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(40)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations

and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(41)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(42)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations

and nested transactions!

Functions/stored procedures

in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(43)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL,

Java, Ruby, Python, and

much more

Online backups

. . .

(44)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures

in SQL, Java,

Ruby, Python, and

much more

Online backups

. . .

(45)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL,

Java, Ruby,

Python, and

much more

Online backups

. . .

(46)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java,

Ruby, Python,

and

much more

Online backups

. . .

(47)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby,

Python, and

much more

Online backups

. . .

(48)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python,

and

much more

Online backups

. . .

(49)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(50)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(51)

Buzzword compliance

What have we got?

JDBC, .NET, ODBC, PHP, Python, Ruby, . . .

Replication

Views, Indexes, Schemas, Foreign keys

Partitioning

Two phase commit

Transations and nested transactions!

Functions/stored procedures in SQL, Java, Ruby, Python, and

much more

Online backups

. . .

(52)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Who uses PostgreSQL?

Businesses, big and small

Hobbyists and developers

Big operators like

Fujitsu

Sun

HP

Apple

Cisco

AC Neilson

Skype

Just about every Australian government department

Safeway (North America)

.org, .info, .in, . . .

Sony

But it’s even more popular with small businesses

(53)

What else can you do?

Report writing: Jasper, Crystal, . . .

Wiki, CMS backend

All the other OSS jazz – forums, photo galleries, . . .

Bugzilla backend

LDAP backend

J2EE backend

Ruby on rails, out of the box

GIS – 100% standards compliant!

Statistical analysis – full integration of ‘R’

Data warehousing

(54)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Outline

1

SELECT ‘Hello World’

2

Hiccups

3

Lets do something

4

Some boring stuff

5

Useful stuff

(55)

Resources for learning more

The PostgreSQL manual

-http://www.postgresql.org/docs

Books (some free)

-http://www.postgresql.org/docs/books/

GUI console -

http://www.pgadmin.org

Mailing lists -

http://www.postgresql.org/

IRC -

irc.freenode.net,

#postgresql

My nick is

swm

(56)

SELECT ‘Hello World’ Hiccups Lets do something Some boring stuff Useful stuff

Stuff for the boss

Case studies:

http://www.postgresql.org/about/casestudies/

Featured users:

http://www.postgresql.org/about/users/

A glossy brochure:

http://alcove.com.au/postgresql.pdf

http://www.postgresql.org/download/ -http://www.postgresql.org/docs -http://www.postgresql.org/docs/books/ http://www.pgadmin.org http://www.postgresql.org/ http://pugs.postgresql.org/sydpug/ http://www.postgresql.org/about/casestudies/ http://www.postgresql.org/about/users/ http://alcove.com.au/postgresql.pdf

References

Related documents

In this view you can easily edit your slide, create a background color, insert a picture, format a picture, insert text and/or format text.. Slide

INSERT query SELECT requires that data types in petal and target tables match The existing records in the target fate are unaffected.. Display the result set himself

Last name to insert into statement inserts it and inserting rows inserted row into a temp table some temporary tables: on the first.. Once experience poor database only boring,

WinCC flexible 2008 Getting Started - First-Time Users Getting Started, 06/2008, Printout of the Online Help.. Table

Within SQL*PLUS, a user can create, alter, or drop tables, to insert tuples to a table, delete tu- ples from a table, update tuples in a table, and to query the database using

Improve segregation of duties by allowing (1) certain users to only view or insert values for Account Flexfields and no other value sets, (2). certain users to only view or

Enter Username &amp; Password SQL Injection Attack Web server Web browser (Client) DB SELECT passwd FROM USERS WHERE uname IS ‘’; DROP TABLE USERS; -- ’.. Eliminates all user

If a catalog proxy is assigned, there is no need to create entries for the user in the permission.dat table (this table is used to define allowed and denied tags for users)..