
Learn PostgreSQL
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
The latest edition of this PostgreSQL book will help you to start using PostgreSQL from absolute scratch, helping you to quickly understand the internal workings of the database. With a structured approach and practical examples, go on a journey that covers the basics, from SQL statements and how to run server-side programs, to configuring, managing, securing, and optimizing database performance.
This new edition will not only help you get to grips with all the recent changes within the PostgreSQL ecosystem but will also dig deeper into concepts like partitioning and replication with a fresh set of examples. The book is also equipped with Docker images for each chapter which makes the learning experience faster and easier. Starting with the absolute basics of databases, the book sails through to advanced concepts like window functions, logging, auditing, extending the database, configuration, partitioning, and replication. It will also help you seamlessly migrate your existing database system to PostgreSQL and contains a dedicated chapter on disaster recovery. Each chapter ends with practice questions to test your learning at regular intervals.
By the end of this book, you will be able to install, configure, manage, and develop applications against a PostgreSQL database.
All prices
More details
Other editions
Previous edition

Content
- Cover
- Copyright
- Contributors
- Table of Contents
- Preface
- Chapter 1: Introduction to PostgreSQL
- Technical requirements
- PostgreSQL at a glance
- A brief history of PostgreSQL
- What's new in PostgreSQL 16?
- PostgreSQL release policy, version numbers, and life cycle
- Exploring PostgreSQL terminology
- Installing PostgreSQL
- What to install
- Installing PostgreSQL from binary packages
- Using the book's Docker images
- Installing PostgreSQL on GNU/Linux Debian, Ubuntu, and derivatives
- Installing PostgreSQL on Fedora Linux
- Installing PostgreSQL on FreeBSD
- Installing PostgreSQL from sources
- Installing PostgreSQL via pgenv
- Summary
- References
- Chapter 2: Getting to Know Your Cluster
- Technical requirements
- Managing your cluster
- pg_ctl
- PostgreSQL processes
- Connecting to the cluster
- The template databases
- The psql command-line client
- Entering SQL statements via psql
- A glance at the psql commands
- Introducing the connection string
- Solving common connection problems
- Database "foo" does not exist
- Connection refused
- No pg_hba.conf entry
- Exploring the disk layout of PGDATA
- Objects in the PGDATA directory
- Tablespaces
- Exploring configuration files and parameters
- Summary
- Verify your knowledge
- References
- Chapter 3: Managing Users and Connections
- Technical requirements
- Introduction to users and groups
- Managing roles
- Creating new roles
- Role passwords, connections, and availability
- Using a role as a group
- Removing an existing role
- Inspecting existing roles
- Managing incoming connections at the role level
- The syntax of pg_hba.conf
- Order of rules in pg_hba.conf
- Merging multiple rules into a single one
- Using groups instead of single roles
- Using files instead of single roles
- Inspecting pg_hba.conf rules
- Including other files in pg_hba.conf
- Summary
- Verify your knowledge
- References
- Chapter 4: Basic Statements
- Technical requirements
- Using the Docker image
- Connecting the database
- Creating and managing databases
- Creating a database
- Managing databases
- Introducing schemas
- PostgreSQL and the public schema
- The search_path variable
- The correct way to start working
- Listing all tables
- Making a new database from a modified template
- Dropping tables and databases
- Dropping tables
- Dropping databases
- Making a database copy
- Confirming the database size
- The psql method
- The SQL method
- Behind the scenes of database creation
- Managing tables
- The EXISTS option
- Managing temporary tables
- Managing unlogged tables
- Creating a table
- Understanding basic table manipulation statements
- Inserting and selecting data
- NULL values
- Sorting with NULL values
- Creating a table starting from another table
- Updating data
- Deleting data
- Summary
- Verify your knowledge
- References
- Chapter 5: Advanced Statements
- Technical requirements
- Exploring the SELECT statement
- Using the like clause
- Using ilike
- Using distinct
- Using limit and offset
- Using subqueries
- Subqueries and the IN/NOT IN condition
- Subqueries and the EXISTS/NOT EXISTS condition
- Learning about joins
- Using INNER JOIN
- INNER JOIN versus EXISTS/IN
- Using LEFT JOINS
- Using RIGHT JOIN
- Using FULL OUTER JOIN
- Using LATERAL JOIN
- Aggregate functions
- UNION/UNION ALL
- EXCEPT/INTERSECT
- Using UPSERT
- UPSERT - the PostgreSQL way
- Learning the RETURNING clause for INSERT
- Returning tuples out of queries
- UPDATE related to multiple records
- MERGE
- Exploring UPDATE ... RETURNING
- Exploring DELETE ... RETURNING
- Exploring CTEs
- CTE concept
- CTE in PostgreSQL greater than 12
- CTE - use cases
- Query recursion
- Recursive CTEs
- Summary
- Verify your knowledge
- References
- Chapter 6: Window Functions
- Technical requirements
- Using basic statement window functions
- Using the PARTITION BY function and WINDOW clause
- Introducing some useful functions
- The ROW_NUMBER function
- The ORDER BY clause
- FIRST_VALUE
- LAST_VALUE
- RANK
- DENSE_RANK
- The LAG and LEAD functions
- The CUME_DIST function
- The NTILE function
- Using advanced statement window functions
- The frame clause
- ROWS BETWEEN start_point and end_point
- RANGE BETWEEN start_point and end_point
- Summary
- Verify your knowledge
- References
- Chapter 7: Server-Side Programming
- Technical requirements
- Exploring data types
- The concept of extensibility
- Standard data types
- Boolean data type
- Numeric data type
- Integer types
- Numbers with a fixed precision data type
- Numbers with an arbitrary precision data type
- Character data type
- Chars with fixed-length data types
- Chars with variable length with a limit data types
- Chars with a variable length without a limit data types
- Date/timestamp data types
- Date data types
- Timestamp data types
- The NoSQL data type
- The hstore data type
- The JSON data type
- Exploring functions and languages
- Functions
- SQL functions
- Basic functions
- SQL functions returning a set of elements
- SQL functions returning a table
- Polymorphic SQL functions
- PL/pgSQL functions
- First overview
- Dropping functions
- Declaring function parameters
- IN/OUT parameters
- Function volatility categories
- Control structure
- Conditional statements
- IF statements
- CASE statements
- Loop statements
- The record type
- Exception handling statements
- Security definer
- Summary
- Verify your knowledge
- References
- Chapter 8: Triggers and Rules
- Technical requirements
- Exploring rules in PostgreSQL
- Understanding the OLD and NEW variables
- Rules on INSERT
- The ALSO option
- The INSTEAD OF option
- Rules on DELETE/UPDATE
- Creating the new_tags table
- Creating two tables
- Managing rules on INSERT, DELETE, and UPDATE events
- INSERT rules
- DELETE rules
- UPDATE rules
- Managing triggers in PostgreSQL
- Trigger syntax
- Triggers on INSERT
- The TG_OP variable
- Triggers on UPDATE / DELETE
- Event triggers
- An example of an event trigger
- Summary
- Verify your knowledge
- References
- Chapter 9: Partitioning
- Technical requirements
- Basic concepts
- Range partitioning
- List partitioning
- Hash partitioning
- Table inheritance
- Dropping tables
- Exploring declarative partitioning
- List partitioning
- Range partitioning
- Partition maintenance
- Attaching a new partition
- Detaching an existing partition
- Attaching an existing table to the parent table
- The default partition
- Partitioning and tablespaces
- A simple case study
- Summary
- Verify your knowledge
- References
- Chapter 10: Users, Roles, and Database Security
- Technical requirements
- Understanding roles
- Properties related to new objects
- Properties related to superusers
- Properties related to replication
- Properties related to RLS
- Changing properties of existing roles: the ALTER ROLE statement
- Renaming an existing role
- SESSION_USER versus CURRENT_USER
- Per-role configuration parameters
- Inspecting roles
- Roles that inherit from other roles
- Understanding how privileges are resolved
- Role inheritance overview
- ACLs
- Default ACLs
- Knowing the default ACLs
- Granting and revoking permissions
- Permissions related to tables
- Column-based permissions
- Permissions related to sequences
- Permissions related to schemas
- ALL objects in the schema
- Permissions related to programming languages
- Permissions related to routines
- Permissions related to databases
- Other GRANT and REVOKE statements
- Assigning the object owner
- Inspecting ACLs
- RLS
- Role password encryption
- SSL connections
- Configuring the cluster for SSL
- Connecting to the cluster via SSL
- Summary
- Verify your knowledge
- References
- Chapter 11: Transactions, MVCC, WALs, and Checkpoints
- Technical requirements
- Introducing transactions
- Comparing implicit and explicit transactions
- Time within transactions
- More about transaction identifiers - the XID wraparound problem
- Virtual and real transaction identifiers
- Multi-version concurrency control
- Transaction isolation levels
- READ UNCOMMITTED
- READ COMMITTED
- REPEATABLE READ
- SERIALIZABLE
- Explaining MVCC
- Savepoints
- Deadlocks
- How PostgreSQL handles persistency and consistency: WALs
- WALs
- WALs as a rescue method in the event of a crash
- Checkpoints
- Checkpoint configuration parameters
- checkpoint_timeout and max_wal_size
- Checkpoint throttling
- Manually issuing a checkpoint
- VACUUM
- Manual VACUUM
- Automatic VACUUM
- Summary
- Verify your knowledge
- References
- Chapter 12: Extending the Database - the Extension Ecosystem
- Technical requirements
- Introducing extensions
- The extension ecosystem
- Extension components
- The control file
- The script file
- Managing extensions
- Creating an extension
- Viewing installed extensions
- Finding out available extension versions
- Altering an existing extension
- Removing an existing extension
- Exploring the PGXN client
- Installing pgxnclient on Debian GNU/Linux and derivatives
- Installing pgxnclient on Fedora Linux and Red Hat-based distributions
- Installing pgxnclient on FreeBSD
- Installing pgxnclient from sources
- The pgxnclient command-line interface
- Installing extensions
- Installing the extension via pgxnclient
- Installing the extension manually
- Using the installed extension
- Removing an installed extension
- Removing an extension via pgxnclient
- Removing a manually compiled extension
- Creating your own extension
- Defining an example extension
- Creating extension files
- Installing the extension
- Creating an extension upgrade
- Performing an extension upgrade
- Summary
- Verify your knowledge
- References
- Chapter 13: Query Tuning, Indexes, and Performance Optimization
- Technical requirements
- Execution of a statement
- Execution stages
- The optimizer
- Nodes that the optimizer uses
- Sequential nodes
- Parallel nodes
- When does the optimizer choose a parallel plan?
- Utility nodes
- Node costs
- Indexes
- Index types
- Creating an index
- Inspecting indexes
- Dropping an index
- Invalidating an index
- Rebuilding an index
- The EXPLAIN statement
- EXPLAIN output formats
- EXPLAIN ANALYZE
- EXPLAIN options
- Examples of query tuning
- ANALYZE and how to update statistics
- Auto-explain
- Summary
- Verify your knowledge
- References
- Chapter 14: Logging and Auditing
- Technical requirements
- Introduction to logging
- Where to log
- When to log
- What to log
- Extracting information from logs - pgBadger
- Installing pgBadger
- Configuring PostgreSQL logging for pgBadger usage
- Using pgBadger
- Scheduling pgBadger
- Implementing auditing
- Installing PgAudit
- Configuring PostgreSQL to exploit PgAudit
- Configuring PgAudit
- Auditing by session
- Auditing by role
- Summary
- Verify your knowledge
- References
- Chapter 15: Backup and Restore
- Technical requirements
- Introducing types of backups and restores
- Exploring logical backups
- Dumping a single database
- Restoring a single database
- Limiting the amount of data to backup
- Compression
- Dump formats and pg_restore
- Performing a selective restore
- Dumping a whole cluster
- Parallel backups
- Backup automation
- The COPY command
- Exploring physical backups
- Performing a manual physical backup
- pg_verifybackup
- Starting the cloned cluster
- Restoring from a physical backup
- Basic concepts behind PITR
- Summary
- Verify your knowledge
- References
- Chapter 16: Configuration and Monitoring
- Technical requirements
- Cluster configuration
- Inspecting all the configuration parameters
- Finding configuration errors
- Nesting configuration files
- Configuration contexts
- Main configuration settings
- WAL settings
- Memory-related settings
- Process information settings
- Networking-related settings
- Archive and replication settings
- Vacuum and autovacuum-related settings
- Optimizer settings
- Statistics collector
- Modifying the configuration from a live system
- Configuration generators
- Monitoring the cluster
- Information about running queries and sessions
- Inspecting locks
- Inspecting databases
- Inspecting tables and indexes
- More statistics
- Advanced statistics with pg_stat_statements
- Installing the pg_stat_statements extension
- Using pg_stat_statements
- Resetting data collected from pg_stat_statements
- Tuning pg_stat_statements
- Summary
- Verify your knowledge
- References
- Chapter 17: Physical Replication
- Technical requirements
- Exploring basic replication concepts
- Physical replication and WALs
- The wal_level directive
- Preparing the environment setup for streaming replication
- Managing streaming replication
- Basic concepts of streaming replication
- Asynchronous replication environment
- The wal_keep_segments option
- The slot way
- The pg_basebackup command
- Asynchronous replication
- Replica monitoring
- Synchronous replication
- PostgreSQL settings
- Cascading replication
- Delayed replication
- Promoting a replica server to a primary
- Summary
- Verify your knowledge
- References
- Chapter 18: Logical Replication
- Technical requirements
- Understanding the basic concepts of logical replication
- Comparing logical replication and physical replication
- Exploring a logical replication setup and new logical replication features on PostgreSQL 16
- Logical replication environment settings
- The replica role
- Primary server - postgresql.conf
- Replica server - postgresql.conf
- The pg_hba.conf file
- Logical replication setup
- Monitoring logical replication
- Read-only versus write-allowed
- DDL commands
- Disabling logical replication
- Making a logical replication using a physical replication instance
- Summary
- Verify your knowledge
- References
- Chapter 19: Useful Tools and Extensions
- Packt page
- Technical requirements
- Exploring the pg_trgm extension
- Using foreign data wrappers and the postgres_fdw extension
- Disaster recovery with pgbackrest
- Basic concepts
- Environment set up
- The exchange of public keys
- Installing pgbackrest
- Configuring pgbackrest
- The repository configuration
- Using pgbackrest with object store support
- The PostgreSQL server configuration
- The postgresql.conf file
- The pgbackrest.conf file
- Creating and managing continuous backups
- Creating the stanza
- Checking the stanza
- Managing base backups
- Managing PITR
- Migrating from MySQL/MariaDB to PostgreSQL using pgloader
- Summary
- Verify your knowledge
- References
- Other Books You May Enjoy
Preface
PostgreSQL is one of the fastest-growing open-source object-relational Database Management Systems (DBMSs) in the world. PostgreSQL provides enterprise-level features; it's scalable, secure, and highly efficient; it's easy to use; and it has a very rich ecosystem that includes application drivers and tools. In this book, you will explore PostgreSQL 16, the latest stable release, and learn to build secure, reliable, and scalable database solutions using it. Complete with hands-on tutorials and a set of Docker images to follow every step-by-step example, this book will teach you how to achieve the right database design for a reliable environment.
You will learn how to install, configure, and manage a PostgreSQL server; manage users and connections; and inspect server activity for performance optimization. With question-and-answer sections for each chapter, you will be able to check your newly acquired knowledge as you go.
The book starts by introducing the main concepts surrounding PostgreSQL and how to install and connect to the database, and then progresses to the management of users, permissions, and basic objects like tables. You will be taught about the Data Definition Language and the most common and useful statements and commands, as well as all the essential relational database concepts, like foreign keys, triggers, and functions. Later, you will explore how to configure and tune your cluster to get the best out of your PostgreSQL service, how to create and manage indexes for fast data retrieval, and how to make and restore backup copies of your data. Lastly, you will learn how to create your own high-availability solution by means of replications, either physical or logical, and you will get a look at some of the most common and useful tools and extensions that you can apply to your cluster.
By the end of this book, you'll be well versed in the PostgreSQL database and be able to set up your own PostgreSQL instance and use it to build robust, data-centric solutions to real-world problems.
Who this book is for
This book is for anyone interested in learning about the PostgreSQL database from scratch or anyone looking to build robust, scalable, and highly available database applications. All the newest and coolest features of PostgreSQL will be presented, along with all the concepts a database administrator or an application developer needs to get the best out of a PostgreSQL instance. Although prior knowledge of PostgreSQL is not required, familiarity with databases and the SQL language is expected.
What this book covers
Chapter 1, Introduction to PostgreSQL, explains what the PostgreSQL database is, the community and development behind this great and robust enterprise-level relational database, and how to get help and recognize different PostgreSQL versions and dependencies. You will also learn how to get and install PostgreSQL either through binary packages or by compiling it from sources. You will see how to manage the cluster with your operating system tools (systemd and rc scripts).
Chapter 2, Getting to Know Your Cluster, shows you the anatomy of a PostgreSQL cluster by specifying what is on the file system, where the main configuration files are, and how they are used. The psql command-line utility is described in order to help you connect to the database cluster and interact with it.
Chapter 3, Managing Users and Connections, provides a complete description of how users and connections are managed by a running instance and how you can prevent or limit users from connecting. The concept of the "role" is described, and you will learn how to create single-user accounts, as well as groups of related users.
Chapter 4, Basic Statements, shows how to create and destroy main database objects, such as databases, tables, and schemas. The chapter also covers basic statements, such as SELECT, INSERT, UPDATE, and DELETE. This chapter shows how to manage the public schema on PostgreSQL 16.
Chapter 5, Advanced Statements, introduces the advanced statements PostgreSQL provides, such as common table expressions, MERGE, UPSERTs, and queries with RETURNING rows. This chapter will provide practical examples of when and how to use them.
Chapter 6, Window Functions, introduces a powerful set of functions that provide aggregation without having to collapse the result in a single row. In other words, thanks to window functions, you can perform aggregation on multiple rows (windows) and still present all the tuples in the output. Window functions allow the implementation of business intelligence and make reporting easy.
Chapter 7, Server-Side Programming, tackles the fact that while SQL is fine for doing most day-to-day work with a database, you could end up with a particular problem that requires an imperative approach. This chapter shows you how to implement your own code within the database, how to write functions and procedures in different languages, and how to make them interact with transaction boundaries.
Chapter 8, Triggers and Rules, presents both triggers and rules with practical examples, showing advantages and drawbacks. The chapter ends with examples about event triggers.
Chapter 9, Partitioning, explores partitioning - splitting a table into smaller pieces. PostgreSQL has supported partitioning for a long time, but with version 10 it introduced so-called "declarative partitioning." This chapter focuses on all the features related to declarative partitioning, its tuning parameters, and how to make a table partitioning using different tablespaces.
Chapter 10, Users, Roles, and Database Security, first looks at user management: roles, groups, and passwords. You will learn how to constrain users to access only particular databases and from particular machines, as well as how to manage permissions associated to users and database objects. You then will see how row-level security can harden your table contents and prevent users from retrieving or modifying tuples that do not belong to them.
Chapter 11, Transactions, MVCC, WALs, and Checkpoints, presents fundamental concepts in PostgreSQL: the Write-Ahead Log (WAL) and the machinery that allows the database to run concurrent transactions and consolidate data in storage. The chapter also presents the concept of transaction isolation, ACID rules, and how the database can implement them. Then you will discover how the WAL can speed up database work and, at the very same time, protect it against crashes. You will understand what MVCC is and why it is important. Lastly, the chapter provides insight into checkpoints and related tunables.
Chapter 12, Extending the Database - the Extension Ecosystem, introduces a handy way to plug new functionalities into your cluster by using so-called "extensions." This chapter will show you what an extension is; how to search for, get, and install a third-party extension; and how to develop your own.
Chapter 13, Query Tuning, Indexes, and Performance Optimization, addresses an important topic for any database administrator: performance. Indexes are fast ways to help the database access the most commonly used data, but they cannot be built on top of everything because of their maintenance costs. The chapter presents the available index types, and then it explains how to recognize tables and queries that could benefit from indexes and how to deploy them. Thanks to tools such as explain and autoexplain, you will keep your queries under control.
Chapter 14, Logging and Auditing, tackles questions such as "What is happening in the database cluster?" and "What happened yesterday?" Having a good logging and auditing ruleset is a key point in the administration of a database cluster. The chapter presents you with the main options for logging, how to inspect logs with external utilities such as pgBadger, and how to audit your cluster (in a way that can help you make it compliant with data regulamentation policies, e.g., GDPR).
Chapter 15, Backup and Restore, explains why having a backup is important, how to take one for all or part of you cluster, and how to restore from a valid backup. The chapter presents the basic and most common ways to back up a single database or a whole cluster, as well as how to do archiving and point-in-time recovery.
Chapter 16, Configuration and Monitoring, presents the cluster configuration options and the PostgreSQL catalogs used to inspect the system from the inside. Different ways to tune the configuration will be presented. Thanks to special extensions, such as pg_stat_activity, you will be able to monitor in real time what your users are doing against the database.
Chapter 17, Physical Replication, covers built-in replication, a mechanism that allows you to keep several instances up and in sync with a single master node, which PostgreSQL has supported since version 9. Replication allows scalability and redundancy, as well as many other scenarios such as testing and comparing databases. This chapter presents so-called "physical replication," a way to fully replicate a whole cluster over another instance that will continuously follow its...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: ePUB
Copy protection: without DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use a reader that can handle the file format ePUB, such as Adobe Digital Editions or FBReader – both free (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePUB works well for novels and non-fiction books – i.e., 'flowing' text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook does not use copy protection or Digital Rights Management
For more information, see our eBook Help page.