O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PostgreSQL: Up and Running, 3rd Edition

Book Description

With Early Release ebooks, you get books in their earliest form—the author's raw and unedited content as he or she writes—so you can take advantage of these technologies long before the official release of these titles. You'll also receive updates when significant changes are made, new chapters are available, and the final ebook bundle is released.

Thinking of migrating to PostgreSQL? This clear, fast-paced introduction helps you understand and use this open source database system. Not only will you learn about the enterprise class features in versions 9.3 to 9.6, with a focus on 9.5, you’ll also discover that PostgeSQL is more than a database system—it’s an impressive application platform as well.

With examples throughout, this book shows you how to achieve tasks that are difficult or impossible in other databases. This third edition covers new features, such as ANSI-SQL constructs found only in proprietary databases until now: PostgreSQL FDW enhancements; new full text functions and operator syntax introduced in version 9.6; row level security; and the UPSERT feature.

If you’re a current PostgreSQL user, you’ll pick up gems you may have missed before.

  • Learn basic administration tasks such as role management, database creation, backup, and restore
  • Apply the psql command-line utility and the pgAdmin graphical administration tool
  • Explore PostgreSQL tables, constraints, and indexes
  • Learn powerful SQL constructs not generally found in other databases
  • Use several different languages to write database functions
  • Tune your queries to run as fast as your hardware will allow
  • Query external and variegated data sources with foreign data wrappers
  • Learn how use built-in replication filters to replicate data

Table of Contents

  1. Preface
    1. Why PostgreSQL
    2. Why Not PostgreSQL?
    3. Audience
    4. For More Information on PostgreSQL
    5. Code and Output Formatting
    6. Conventions Used in This Book
    7. Using Code Examples
    8. Safari ® Books Online
    9. How to Contact Us
  2. 1. The Basics
    1. Where to Get PostgreSQL
    2. Administration Tools
      1. psql
      2. pgAdmin
      3. phpPgAdmin
      4. Adminer
    3. PostgreSQL Database Objects
    4. What’s New in Latest Versions of PostgreSQL?
      1. Why Upgrade?
      2. What’s coming in PostgreSQL 10
      3. Features introduced in PostgreSQL 9.6
      4. Features Introduced in PostgreSQL 9.5
      5. Features Introduced in PostgreSQL 9.4
    5. Database Drivers
    6. Where to Get Help
    7. Notable PostgreSQL Forks
  3. 2. Database Administration
    1. Configuration Files
      1. Making Configurations Take Effect
      2. The postgresql.conf file
      3. The pg_hba.conf file
    2. Managing Connections
    3. Check for Queries being blocked
    4. Roles
      1. Creating Login Roles
      2. Creating Group Roles
    5. Database Creation
      1. Template Databases
      2. Using Schemas
    6. Privileges
      1. Types of Privileges
      2. Getting Started
      3. GRANT
      4. Default Privileges
      5. Privilege Idiosyncrasies
    7. Extensions
      1. Installing Extensions
      2. Common Extensions
    8. Backup and Restore
      1. Selective Backup Using pg_dump
      2. Systemwide Backup Using pg_dumpall
      3. Restoring data
    9. Managing Disk Storage with Tablespaces
      1. Creating Tablespaces
      2. Moving Objects Among Tablespaces
    10. Verboten Practices
      1. Don’t Delete PostgreSQL Core System Files and Binaries
      2. Don’t Grant Full OS Administrative privileges to the Postgres System Account (postgres)
      3. Don’t Set shared_buffers Too High
      4. Don’t Try to Start PostgreSQL on a Port Already in Use
  4. 3. psql
    1. Environment Variables
    2. Interactive versus Noninteractive psql
    3. psql Customizations
      1. Custom Prompts
      2. Timing Executions
      3. Autocommit Commands
      4. Shortcuts
      5. Retrieving Prior Commands
    4. psql Gems
      1. Executing Shell Commands
      2. Watching Statements
      3. Retrieving Details of Database Objects
      4. Crosstabs
      5. Dynamic SQL Execution
    5. Importing and Exporting Data
      1. psql Import
      2. psql Export
      3. Copying from or to Program
    6. Basic Reporting
  5. 4. Using pgAdmin
    1. Getting Started
      1. Overview of Features
      2. Connecting to a PostgreSQL Server
      3. Navigating pgAdmin
    2. pgAdmin Features
      1. Accessing psql from pgAdmin
      2. Editing postgresql.conf and pg_hba.conf from pgAdmin
      3. Creating Database Assets and Setting Privileges
      4. Import and Export
      5. Backup and Restore
    3. pgScript
    4. Graphical Explain
    5. Job Scheduling with pgAgent
      1. Installing pgAgent
      2. Scheduling Jobs
      3. Helpful pgAgent Queries
  6. 5. Data Types
    1. Numerics
      1. Serials
      2. Generate Series Function
    2. Textuals
      1. String Functions
      2. Splitting Strings into Arrays, Tables, or Substrings
      3. Regular Expressions and Pattern Matching
    3. Temporals
      1. Time Zones: What They Are and Are Not
      2. Datetime Operators and Functions
    4. Arrays
      1. Array Constructors
      2. Unnesting Arrays to Rows
      3. Array Slicing and Splicing
      4. Referencing Elements in an Array
      5. Array Containment Checks
    5. Range Types
      1. Discrete Versus Continuous Ranges
      2. Built-in Range Types
      3. Defining Ranges
      4. Defining Tables with Ranges
      5. Range Operators
    6. JSON
      1. Inserting JSON Data
      2. Querying JSON
      3. Outputting JSON
      4. Binary JSON: jsonb
      5. Editing JSONB data
    7. XML
      1. Inserting XML Data
      2. Querying XML Data
    8. Full Text Search
      1. FTS Configurations
      2. TSVectors
      3. TSQueries
      4. Using Full Text Search
      5. Ranking Results
      6. Full Text Stripping
    9. Custom and Composite Data Types
      1. All Tables Are Custom Data Types
      2. Building Custom Data Types
      3. Building Operators and Functions for Custom Types
  7. 6. Tables, Constraints, and Indexes
    1. Tables
      1. Basic Table Creation
      2. Inherited Tables
      3. Unlogged Tables
      4. TYPE OF
    2. Constraints
      1. Foreign Key Constraints
      2. Unique Constraints
      3. Check Constraints
      4. Exclusion Constraints
    3. Indexes
      1. PostgreSQL Stock Indexes
      2. Operator Classes
      3. Functional Indexes
      4. Partial Indexes
      5. Multicolumn Indexes
  8. 7. SQL: The PostgreSQL Way
    1. Views
      1. Single Table Views
      2. Using Triggers to Update Views
      3. Materialized Views
    2. Handy Constructions
      1. DISTINCT ON
      2. LIMIT and OFFSET
      3. Shorthand Casting
      4. Multi-row Insert
      5. ILIKE for Case-Insensitive Search
      6. ANY Array Search
      7. Set-returning Functions in SELECT
      8. Restricting DELETE, UPDATE, and SELECT from Inherited Tables
      9. DELETE USING
      10. Returning Affected Records to the User
      11. Composite Types in Queries
      12. Dollar Quoting
      13. DO
      14. FILTER Clause for Aggregates
      15. Percentiles and Mode
    3. Window Functions
      1. PARTITION BY
      2. ORDER BY
    4. Common Table Expressions
      1. Basic CTEs
      2. Writable CTEs
      3. Recursive CTE
    5. Lateral Joins
    6. WITH ORDINALITY
    7. GROUPING SETS, CUBE, ROLLUP
  9. 8. Writing Functions
    1. Anatomy of PostgreSQL Functions
      1. Function Basics
      2. Triggers and Trigger Functions
      3. Aggregates
      4. Trusted and Untrusted Languages
    2. Writing Functions with SQL
      1. Basic SQL Function
      2. Writing SQL Aggregate Functions
    3. Writing PL/pgSQL Functions
      1. Basic PL/pgSQL Function
      2. Writing Trigger Functions in PL/pgSQL
    4. Writing PL/Python Functions
      1. Basic Python Function
    5. Writing PL/V8, PL/CoffeeScript, and PL/LiveScript Functions
      1. Basic Functions
      2. Writing Aggregate Functions with PL/V8
      3. Writing Window Functions in PL/V8
  10. 9. Replication and External Data
    1. Replication Overview
      1. Replication Jargon
      2. Evolution of PostgreSQL Replication
      3. Third-Party Replication Options
    2. Setting Up Replication
      1. Configuring the Master
      2. Configuring the Slaves
      3. Initiating the Replication Process
    3. Foreign Data Wrappers
      1. Querying Flat Files
      2. Querying a Flat File as Jagged Arrays
      3. Querying Other PostgreSQL Servers
      4. Querying Other Tabular Formats with ogr_fdw
      5. Querying Nonconventional Data Sources