Cover image for PostgreSQL: Up and Running, 2nd Edition

Book description

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 the 9.3 release (with highlights of 9.4), you’ll also discover that PostgeSQL is more than just a database system—it’s also an impressive application platform.

Table of Contents

  1. Preface
    1. Audience
    2. What Makes PostgreSQL Special and Why Use It?
    3. Why Not PostgreSQL?
    4. Where to Get Data and Code Used in This Book
    5. For More Information on PostgreSQL
    6. Code and Output Formatting
    7. Conventions Used in This Book
    8. Using Code Examples
    9. Safari ® Books Online
    10. 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 Assets
    4. What’s New in Latest Versions of PostgreSQL?
      1. Why Upgrade?
      2. What’s coming in PostgreSQL 9.4
      3. PostgreSQL 9.3: New Features
      4. PostgreSQL 9.2: New Features
      5. PostgreSQL 9.1: New Features
    5. Database Drivers
    6. Where to Get Help
    7. Notable PostgreSQL Forks
  3. 2. Database Administration
    1. Configuration Files
      1. File
        1. “I edited my postgresql.conf and now my server is broken.”
      2. File
        1. “I edited my pg_hba.conf and now my server is broken.”
        2. Authentication Methods
      3. Reloading the Configuration Files
    2. Managing Connections
    3. Roles
      1. Creating Login Roles
      2. Creating Group Roles
        1. Inheriting Rights from Group Roles
    4. Database Creation
      1. Template Databases
      2. Using Schemas
    5. Privileges
      1. Privilege: Types of Privileges
      2. Getting Started
      3. GRANT
      4. Default Privileges
      5. Privilege Idiosyncrasies
    6. Extensions
      1. Installing Extensions
        1. Step 1: Installing on the Server
        2. Step 2: Installing into a Database (pre-9.1)
        3. Step 2: Installing into a Database (9.1 forward)
        4. Upgrading to the New Extension Model
      2. Common Extensions
        1. Popular Extensions
        2. Classic Extensions
    7. Backup and Restore
      1. Selective Backup Using
      2. Systemwide Backup Using
      3. Restore
        1. Using PSQL to Restore Plain Text SQL Backups
        2. Using
    8. Managing Disk Storage with Tablespaces
      1. Creating Tablespaces
      2. Moving Objects Between Tablespaces
    9. Verboten Practices
      1. Don’t Delete PostgreSQL Core System Files and Binaries
      2. Don’t Give Full OS Administrative Rights 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. Interactive psql
    2. Non-Interactive psql
    3. Session Configurations
      1. Changing Prompts
      2. Timing Details
      3. AUTOCOMMIT
      4. Shortcuts
      5. Retrieving Prior Commands
    4. psql Gems
      1. Executing Shell Commands
      2. Watching Statements
      3. Lists and Structures
    5. Importing and Exporting Data
      1. COPY FROM / 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 Databases and Setting Permissions
        1. Creating Databases and Other Objects
        2. Permission Management
      4. Import and Export
        1. Importing files
        2. Exporting queries as a structured file or report
      5. Backup and Restore
        1. Backing up a whole database
        2. Backing up system-wide objects
        3. Selective Backup of Database Objects
    3. pgScript
    4. Graphical Explain
    5. Job Scheduling with pgAgent
      1. Installing pgAgent
      2. Scheduling Jobs
      3. Helpful Queries
  6. 5. Data Types
    1. Numerics
      1. Serials
      2. Generate Series Function
    2. Characters and Strings
      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. Referencing Elements in An Array
      3. Array Slicing and Splicing
      4. Unnesting Arrays to Rows
    5. Range Types
      1. Discrete versus Continuous Ranges
      2. Built-in Range Types
      3. Defining Ranges
      4. Defining Tables with Ranges
      5. Range Operators
        1. Overlap Operator
        2. Contains and Contained in Operators
    6. JSON
      1. Inserting JSON Data
      2. Querying JSON
      3. Outputting JSON
      4. Binary JSON: jsonb
    7. XML
      1. Inserting XML Data
      2. Querying XML Data
    8. 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. Window Functions
      1. Partition By
      2. Order By
    3. Common Table Expressions
      1. Standard CTE
      2. Writeable CTEs
      3. Recursive CTE
    4. LATERAL
    5. Handy Constructions
      1. DISTINCT ON
      2. LIMIT and OFFSET
      3. Multi-Row Insert
      4. Shorthand Casting
      5. ILIKE for Case Insensitive Search
      6. Set Returning Functions in SELECT
      7. Selective DELETE, UPDATE, and SELECT from Inherited Tables
      8. DELETE USING
      9. Returning Affected Records
      10. Composite Types in Queries
      11. Using DO
  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
  10. 9. Query Performance Tuning
    1. EXPLAIN and EXPLAIN ANALYZE
    2. Gathering statistics on Statements
    3. Writing Better Queries
      1. Overusing Subqueries in SELECT
      2. Avoid SELECT *
      3. Make Good Use of CASE
    4. Guiding the Query Planner
      1. Strategy Settings
      2. How Useful Is Your Index?
      3. Table Stats
      4. Random Page Cost and Quality of Drives
    5. Caching
  11. 10. 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 (FDW)
      1. Querying Simple Flat File Data Sources
      2. Querying Flat File Data Sources as Jagged Arrays
      3. Querying other PostgreSQL servers
      4. Querying More Complex Data Sources
  12. A. Installing PostgreSQL
    1. Windows, Desktop Linux
    2. CentOS, Fedora, Red Hat, Scientific Linux
    3. Debian, Ubuntu
    4. FreeBSD
    5. Mac OS X
  13. B. PostgreSQL Packaged Command-Line Tools
    1. Database Backup using pg_dump
    2. Server Backup: pg_dumpall
    3. Database Restore: pg_restore
    4. psql Interactive Commands
    5. psql Non-Interactive Commands
  14. About the Authors
  15. Index
  16. Copyright