Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
www.citusdata.com
C O NT E NT S
071
About PostgreSQL
Configuration
Data Types
Commonly Used Functions
Essential PostgreSQL
By Leo Hsu and Regina Obe
ESSENTIAL POSTGRESQL
PURPOSE
postgresql.conf
pg_hba.conf
pg_indent.conf
port
search_path
constraint_
exclusion
shared_buffers
PG_HBA.CONF
PostgreSQL supports many authentication schemes to control
access to the database. The pg_hba.conf file dictates which
schemes are used based on the rules found in this file. You
can mix and match various authentication schemes at the
Real-Time
Big Data Using
PostgreSQL
Deliver real-time insights into your
big data to your end users while
reducing the cost and complexity
of your database infrastructure
POSTGRESQL.CONF
The following settings are all located in the postgresql.conf
file. Remember that these are default settings; many of these
you can choose to override for each session, for each database,
or for each user/role.
D Z O NE, INC.
listen_
addresses
SELECT pg_reload_conf();
FILE
DESCRIPTION
CO N F I G U R AT I O N
HOT
TIP
OPTION
www.citusdata.com
DZ O NE .C O M
2
same time. The rules are applied sequentially such that the
first match fitting a connection is the one that is used. This is
important to remember because if you have a more restrictive
rule above a less restrictive, then the more restrictive is the one
that trumps.
NUMERIC TYPES
DATABASE
USER
CIDR-ADDRESS
ESSENTIAL POSTGRESQL
TYPE
DESCRIPTION
int, int8
serial, serial4,
serial8
numeric(s, p)
double precision
numrange,
int4range
METHOD
HOST
ALL
ALL
127.0.0.1/32
TRUST
HOST
ALL
ALL
0.0.0.0/0
MD5
SELECT [1,10)::int4range;
percentile_cont,
percentile_disc
DATA T Y P E S
PostgreSQL has numerous built-in types. In addition, you can
define custom types. Furthermore, all tables are considered to
be types in their own right, and can therefore be used within
another tables column. Below are the common built-in types:
STRING TYPES
DATE/TIME TYPES
TYPE
DESCRIPTION
varchar(n) (a.k.a.
character varying)
TYPE
DESCRIPTION
char(n)
Padded to n characters.
date
text
Unlimited text.
timestamp
OTHER TYPES
TYPE
DESCRIPTION
array
time
SELECT ARRAY['john','jane'];
SELECT ARRAY(SELECT emp_name FROM employees);
SELECT array_agg(e.emp_name) FROM employees;
Example: 23:14:20
time with time
zone
Example: 23:14:20-04
interval
enum
Constituents of
datetime, use
date_part function
to extract
D Z O NE, INC .
Enumerators:
CREATE TYPE cloth_colors AS ENUM
('red','blue','green');
boolean
True/false.
bytea
lo
JSON
DZ O NE .C O M
3
TYPE
DESCRIPTION
jsonb
TYPE
TYPE
DESCRIPTION
CURRENT_
TIMESTAMP, now()
CURRENT_DATE
CURRENT_TIME
age(timestamp1,
timestamp2)
age(timestamp)
operators +, -, / (for
intervals only)
generate_
series(timestamp,
timestamp, [interval]) [8.4]
quote_nullable
replace
string_agg
strpos(text, subtext)
Array concatenation.
|| (string || string,
string || number)
String concatenation.
length
array_upper(anyarray,
dimension)
array_lower(anyarray,
dimension)
SELECT array_upper(ARRAY[ARRAY['a'],
ARRAY['b']], 1);
outputs: 2
array_to_
string(anyarray,
delimiter_text)
DESCRIPTION
@>
&&
D Z O NE, INC .
md5
array_agg
TYPE
DESCRIPTION
TYPE
unnest
DESCRIPTION
||
lpad, rpad
TYPE
quote_literal
date_part('day', timestamp
'2009-07-04 11:05:45') => 4
date_trunc(text, timestamp
| timestamptz | date)
split_part
CO M M O N LY U S E D F U N C T I O N S
TYPE
DESCRIPTION
quote_ident
date_part(text, timestamp),
date_part(text, interval)
ESSENTIAL POSTGRESQL
DZ O NE.C O M
4
TYPE
OTHER FUNCTIONS
DESCRIPTION
TYPE
Union of 2 ranges.
+
DESCRIPTION
generate_series(int1, int2,
[step])
SELECT '[2014-7-20,
2014-10-20]'::daterange + '[2014-6-20,
2014-7-22]'::daterange;
[2014-06-20,2014-10-21)
generate_
series(timestamp1,
timestamp2, [interval])
Intersection.
Common aggregates.
SELECT '[2014-7-20,
2014-10-20]'::daterange * '[2014-6-20,
2014-7-22]'::daterange;
percentile_dist, percentile_
cont [9.4]
Output:
ESSENTIAL POSTGRESQL
Output:
[2014-07-20,2014-07-23)
DATA B A S E O B J E C TS
Difference.
-
SELECT '[2014-7-20,
2014-10-20]'::daterange - '[2014-6-20,
2014-7-22]'::daterange;
Output:
[2014-07-20,2014-10-21)
DESCRIPTION
OB JECT
DESCRIPTION
Databases*
Tablespaces*
Languages
Casts
Schemas
Tables, Views
Rules
Functions,
Triggers, and
Aggregates
->
#>>
SELECT prod#>>'{nutrition,vitamin
d}'::text[] AS vd
FROM (
SELECT '{"id": 1,"name": "milk",
"price": 2.50, "nutrition": {"vitamin
d": "30%"}}'::json AS prod) AS f;
Outputs: 30%
#>
WINDOW FUNCTIONS
TYPE
DESCRIPTION
row_number
rank, percent_
rank, dense_
rank
lead, lag
first_value, last_
value, nth_value
D Z O NE, INC .
DZ O NE.C O M
5
OB JECT
DESCRIPTION
Functions,
Triggers, and
Aggregates (cont.)
Operators,
Operator Classes,
Operator Families
Sequences
Types
Foreign Data
Wrappers,
Servers and User
Mappings
Extensions
[9.1+]
ESSENTIAL POSTGRESQL
DESCRIPTION
-d
-h
-p
-U
TOOLS
Execute an SQL batch script against a database and send output to file
DESCRIPTION
psql
createdb,
dropdb
pgAdminIII
pg_restore
pg_dump
pg_dumpall
pgAgent
Once you are in the PSQL terminal there are a myriad of tasks
you can perform. Below are some of the common ones.
pg_upgrade
PS Q L CO M M O N TA S K S
PSQL is a command-line tool that allows you to run ad-hoc
queries, scripts, and other useful database management routines.
D Z O NE, INC .
COM M AND
TA SK
\q
Quit
:q
\?
\h some_command
\connect postgres
Switch database
\l
\dtv p*
\du
\d sometable
\i somefile
\o somefile
DZ O NE.C O M
ESSENTIAL POSTGRESQL
COM M AND
TA SK
SWITCH
\timing
-b, --blobs
\copy
-d, --dbname=NAME
DR
-F, --format=c|t|p|d
TOOL
-c, --clean
DRA
A D M I N TA S K S
BACKUP AND RESTORE
Below are common backup and restore statements.
-j, --jobs=NUM
[8.4],[9.2]
DR
-L, --uselist=filename
-n, --schema=NAME
DR
-N, --excludeschema=SCHEMA
DR
-r, --roles-only
-t, --table=NAME
-T, --excludetable=NAME
-v --verbose
OTHER
Change globally work mem (9.4+)
Requires reload and some require restart.
ALTER SYSTEM SET work_mem TO '20MB';
SELECT pg_reload_conf();
HOT
TIP
Controls verbosity.
Exclude dumping table data for
specific table.
-s section=predata|postdata|data [9.2]
DR
--if-exists [9.4]
D Z O NE, INC .
DRA
--exclude-tabledata=TABLE [9.2]
-l, --list
-g, --globals-only
DESCRIPTION
DZ O NE.C O M
7
Create a group role with no
login rights and members
inherit rights of role
Example uses:
Create or alter a
view
DATA D E F I N I T I O N (D D L)
Create a
materialized view
Refresh
materialized view
Refresh
materialized view
without read
blocking [9.4]
Create a view
(doesnt allow
insert if data would
not be visible in
view) [9.4]
Add a column to a
table
Add a functional
index to a table
Create a trigger
Install extension in
a database
Create a schema
Changing database
schema search path
Dropping
objects with no
dependencies
Suppress redundant
updates
Create a table
Create a check
constraint
D Z O NE, INC .
Revoke rights
ESSENTIAL POSTGRESQL
HOT
TIP
Q U E RY A N D U P DAT E (D M L)
These are examples that show case some of PostgreSQL popular
or unique query features.
DZ O NE.C O M
8
Insert statement
from SELECT, only
load items not
already in table
UPDATE tableA
SET price = invnew.price
FROM tableB AS invnew
WHERE invnew.id = tableA.id
AND NOT (invnew.price = tableA.price);
Insert from a
comma-delimited
file with header
row
Copy data to
comma-delimited
file and include
header
RETRIEVING DATA
View running
queries
Use window
function to
number records
and get running
average
Ordered
aggregates, list
scores in order of
test date, one row
for each student.
Cast to make a
string.
SELECT student,
string_agg(score::text, ',' ORDER BY
test_date DESC) AS scores
FROM test_scores
GROUP BY student;
Non-Recursive
CTE with 2 CTE
expressions. Note
a CTE expression
has only one
WITH, each
subexpression is
separated by a ,
and the final query
follows.
WITH c AS
( SELECT country_code, conv_us
FROM country
WHERE country IN('Japan', 'USA','Germany')
),
prices AS
(SELECT p.car, p.fuel_grade, price*c.
conv_us AS us_price
FROM cars AS p
INNER JOIN c
ON p.country_code = c.country_
code
WHERE p.category = 'Cars'
)
SELECT DISTINCT ON(fuel_grade)
prices.car, us_price
FROM prices
ORDER BY fuel_grade, us_price;
qtr_start
| lhsu
| robe
------------+---------+-----2014-01-01 | {99}
| {95}
2014-07-01 | {92,97} | {98}
student | scores
---------+---------lhsu
| 97,92,99
robe
| 98,95
P RO C E D U R A L L A N G U A G E S
PostgreSQL stands out from other databases with its extensive
and extendable support for different languages to write
database-stored functions. It allows you to call out to libraries
native to that language. We will list the key language as well as
some esoteric ones. The languages with an * are preinstalled
with PostgreSQL and can be enabled. Some require further
installs in addition to the language handler.
You can create set returning functions, simple scalar functions,
triggers, and aggregate functions with most of these languages.
This allows for languages that are highly optimized for a
particular task to work directly with data without having to
always copy it out to process as you normally would need with a
simple database storage device. Language handlers can be of two
flavors: trusted and untrusted. An untrusted language can access
the filesystem directly.
D Z O NE, INC .
SELECT date_trunc('quarter',test_
date)::date AS qtr_start,
array_agg(score) FILTER (WHERE student
= 'lhsu') AS lhsu,
array_agg(score) FILTER (WHERE student
= 'robe') AS robe
FROM test_scores
GROUP BY date_trunc('quarter',test_date);
Recursive CTE *
inventory, gives
full name which
includes parent
tree name e.g.
Paper->Color>Red->20 lbs
Filtered
aggregates [9.4]
use instead of
CASE WHEN
(or subselect)
(especially useful
for aggregates like
array_agg which
may return nulls
with CASE WHEN)
ESSENTIAL POSTGRESQL
DZ O NE.C O M
9
From PostgreSQL 9.1+, languages not enabled by default in
database or not built-in are installed using :
EXAMPLE FUNCTIONS
This next table demonstrates some examples of writing
functions in various languages. For all functions you write, you
can use the CREATE or REPLACE FUNCTION construction to overwrite
existing functions that take same arguments. We use CREATE
FUNCTION here.
CREATE EXTENSION ;
CREATE EXTENSION 'plpythonu';
CREATE OR REPLACE somename(arg1 arg1type)
RETURNS result_argtype AS
$$
body goes here
$$
LANGUAGE 'somelang';
L ANGUAGE
sql*
(trusted)
c*
DESCRIPTION
REQ
plperl (trusted),
plperlu (untrusted)
plpythonu,
plpython2u,
plpython3u (untrusted)
plr
plv8
none
CREATE FUNCTION
fib(n int) RETURNS int AS $$
function fib(n) {
return n<2 ? n : fib(n-1) +
fib(n-2)
}
return fib(n)
$$ LANGUAGE plv8 IMMUTABLE STRICT;
plv8 (trusted)
E X A M PLE
L ANGUAGE
none
plpgsql*
(trusted)
ESSENTIAL POSTGRESQL
none
Google
v8
engine
To install:
CREATE EXTENSION plv8;
CREATE EXTENSION plls;
CREATE EXTENSION plcoffee;
D Z O NE, INC .
DZ O NE.C O M
10
ESSENTIAL POSTGRESQL
EXTENSIONS
Extensions extend the capabilities of PostgreSQL by providing
additional data types, functions, index types, and more. After
installing an extension, you need to run the following command
to enable it:
CREATE EXTENSION extension_name;
E XTENSION
DESCRIPTION
LINK
pg_stat_
statements
http://www.
postgresql.org/
docs/current/static/
pgstatstatements.
html
cstore_fdw
https://github.com/
citusdata/cstore_fdw
postgresqlhll
https://github.com/
aggregateknowledge/
postgresql-hll
pgcrypto
Cryptographic functions.
http://www.
postgresql.org/
docs/current/static/
pgcrypto.html
dblink
Connections to other
PostgreSQL databases from
a database session.
http://www.
postgresql.org/
docs/current/static/
dblink.html
NOTABLE EXTENSIONS
E XTENSION
DESCRIPTION
LINK
PostGIS
http://postgis.net/
pg_shard
https://github.com/
citusdata/pg_shard
HOT
TIP
RECOMMENDED BOOK
The wife and husband team of Leo Hsu and Regina Obe founded
Paragon Corporation in 1997, which specializes in database
technology and works with numerous organizations to design,
develop, and maintain database and web applications. They have
become active participants in the on-going development of PostGIS,
a spatial extension of PostgreSQL. Regina is a member of the PostGIS
core development team and Project Steering Committee. They
maintain two sites: http://www.postgresonline.com -- provides tips
and tricks for using PostgreSQL and http://www.bostongis.com - provides tips and
tricks for using PostGIS and other open source and open GIS tools.
BUY NOW
CREDITS:
Editor: G. Ryan Spain | Designer: Yassee Mohebbi | Production: Chris Smith | Sponsor Relations: Brandon Rosser | Marketing: Chelsea Bosworth
COMMUNITIES:
Share links, author articles, and engage with other tech experts
JOIN NOW
DZONE, INC.
150 PRESTON EXECUTIVE DR.
CARY, NC 27513
DZone communities deliver over 6 million pages each month to more than 3.3 million software
developers, architects and decision makers. DZone offers something for everyone, including news,
tutorials, cheat sheets, research guides, feature articles, source code and more.
888.678.0399
919.678.0300
REFCARDZ FEEDBACK WELCOME
refcardz@dzone.com
Copyright 2015 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any
DZONE,
DZONE, INC.
INC.
form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
SPONSORSHIP OPPORTUNITIES
DZONE.COM
DZONE.COM
sales@dzone.com
VERSION 1.0
$7.95