PostgreSQL 13 Cookbook

Book description

Get to grips with building reliable, scalable, and maintainable database solutions for enterprises and production databases

Key Features

  • Implement PostgreSQL 13 features to perform end-to-end modern database management
  • Design, manage, and build enterprise database solutions using a unique recipe-based approach
  • Solve common and not-so-common challenges faced while working to achieve optimal database performance

Book Description

PostgreSQL has become the most advanced open source database on the market. This book follows a step-by-step approach, guiding you effectively in deploying PostgreSQL in production environments.

The book starts with an introduction to PostgreSQL and its architecture. You'll cover common and not-so-common challenges faced while designing and managing the database. Next, the book focuses on backup and recovery strategies to ensure your database is steady and achieves optimal performance. Throughout the book, you'll address key challenges such as maintaining reliability, data integrity, a fault-tolerant environment, a robust feature set, extensibility, consistency, and authentication. Moving ahead, you'll learn how to manage a PostgreSQL cluster and explore replication features for high availability. Later chapters will assist you in building a secure PostgreSQL server, along with covering recipes for encrypting data in motion and data at rest. Finally, you'll not only discover how to tune your database for optimal performance but also understand ways to monitor and manage maintenance activities, before learning how to perform PostgreSQL upgrades during downtime.

By the end of this book, you'll be well-versed with the essential PostgreSQL 13 features to build enterprise relational databases.

What you will learn

  • Understand logical and physical backups in Postgres
  • Demonstrate the different types of replication methods possible with PostgreSQL today
  • Set up a high availability cluster that provides seamless automatic failover for applications
  • Secure a PostgreSQL encryption through authentication, authorization, and auditing
  • Analyze the live and historic activity of a PostgreSQL server
  • Understand how to monitor critical services in Postgres 13
  • Manage maintenance activities and performance tuning of a PostgreSQL cluster

Who this book is for

This PostgreSQL book is for database architects, database developers and administrators, or anyone who wants to become well-versed with PostgreSQL 13 features to plan, manage, and design efficient database solutions. Prior experience with the PostgreSQL database and SQL language is expected.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. PostgreSQL 13 Cookbook
  3. Contributors
    1. About the author
    2. About the reviewers
  4. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the color images
    5. Conventions used
    6. Sections
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. There's more…
    11. See also
    12. Get in touch
    13. Reviews
  5. Cluster Management Fundamentals
    1. Technical requirements
    2. Installing PostgreSQL 13 using RPMs on CentOS
    3. Getting ready
    4. How to do it...
    5. How it works...
    6. Initializing a PostgreSQL cluster using initdb
    7. Getting ready 
    8. How to do it...
    9. How it works...
    10. Starting a PostgreSQL cluster using pg_ctl 
    11. Getting ready
    12. How to do it...
    13. How it works...
    14. Clusters in PostgreSQL
    15. Databases in PostgreSQL
    16. There's more...
    17. Shutting down a PostgreSQL cluster using different shutdown modes
    18. Getting ready
    19. How to do it...
    20. How it works...
    21. There's more...
    22. Identifying a PostgreSQL data directory and its contents
    23. Getting ready
    24. How to do it...
    25. How it works...
    26. There's more...
    27. Moving pg_wal to another location
    28. Getting ready
    29. How to do it...
    30. How it works...
    31. Running the psql client and some psql shortcuts
    32. Getting ready
    33. How to do it...
    34. How it works...
    35. Running a SQL server using psql
    36. Getting a list of databases
    37. Finding the database's size
    38. Connecting to a database
    39. Getting the list of schemas in a database
    40. Getting the list of tables
    41. Describing a table
    42. There's more...
    43. SQLs behind the shortcuts
    44. Locating the Postgres configuration file
    45. Getting ready
    46. How to do it...
    47. How it works...
    48. There's more...
    49. include_dir
    50. include
    51. Modifying the location of a postgresql.conf file in PostgreSQL
    52. Getting ready
    53. How to do it...
    54. How it works...
    55. Modifying the postgresql.auto.conf file in PostgreSQL
    56. Getting ready
    57. How to do it...
    58. How it works...
    59. There's more...
    60. Enable archiving in PostgreSQL
    61. Getting ready
    62. How to do it...
    63. How it works...
    64. There's more...
  6. Cluster Management Techniques
    1. Technical requirements
    2. Creating and dropping databases
    3. Getting ready
    4. How to do it...
    5. How it works
    6. There's more
    7. Locating a database and a table on the file system
    8. Getting ready
    9. How to do it...
    10. How it works...
    11. Creating a schema in PostgreSQL
    12. Getting ready
    13. How to do it...
    14. How it works...
    15. There's more...
    16. Checking table and index sizes in PostgreSQL
    17. Getting ready
    18. How to do it...
    19. How it works...
    20. There's more...
    21. Creating tablespaces
    22. Getting ready
    23. How to do it...
    24. How it works
    25. There's more...
    26. Moving tables to a different tablespace
    27. Getting ready
    28. How to do it...
    29. How it works
    30. Creating a user in PostgreSQL
    31. Getting ready
    32. How to do it...
    33. How it works
    34. There's more...
    35. Dropping a user in PostgreSQL
    36. Getting ready
    37. How to do it ...
    38. How it works ...
    39. Assigning and revoking a privilege to/from a user or a role 
    40. Getting ready
    41. How to do it
    42. How it works
    43. Creating a group role for role-based segregation 
    44. Getting ready
    45. How to do it
    46. How it works
    47. MVCC implementation and VACUUM in PostgreSQL
    48. Getting ready
    49. How to do it...
    50. How it works...
    51. tableoid
    52. xmin
    53. xmax
    54. ctid
    55. pageinspect
    56. There's more...
  7. Backup and Recovery
    1. Technical requirements
    2. Backing up and restoring a database using pg_dump and pg_restore
    3. Getting ready 
    4. The RPM package for CentOS/Red Hat distributions
    5. Debian and Ubuntu
    6. How to do it
    7. Section A
    8. Section B
    9. How it works 
    10. Backing up and restoring one or more tables using pg_dump and pg_restore
    11. Getting ready
    12. How to do it
    13. How it works
    14. Backing up and restoring globals or an entire cluster using pg_dumpall and psql
    15. Getting ready
    16. How to do it
    17. How it works...
    18. Parallel backup and restore using pg_dump and pg_restore
    19. Getting ready
    20. How to do it
    21. How it works
    22. Backing up a database cluster using pg_basebackup
    23. Getting ready
    24. How to do it
    25. How it works...
    26. Restoring a backup taken using pg basebackup
    27. Getting ready
    28. How to do it
    29. How it works
    30. Installing pgBackRest on CentOS/RedHat OS
    31. Getting ready
    32. How to do it
    33. How it works
    34. Installing pgBackRest on Ubuntu/Debian OS
    35. Getting ready
    36. How to do it
    37. How it works
    38. Backing up a database cluster using pgBackRest
    39. Getting ready
    40. How to do it
    41. How it works
    42. Restoring a backup taken using pgBackRest
    43. Getting ready
    44. How to do it
    45. How it works
  8. Advanced Replication Techniques
    1. Setting up streaming replication in PostgreSQL 13
    2. Getting ready...
    3. How to do it ...
    4. How it works ...
    5. Adding a delayed standby for faster point-in-time recovery
    6. Getting ready...
    7. How to do it...
    8. How it works...
    9. Promoting a standby to a master
    10. Getting ready...
    11. How to do it...
    12. How it works...
    13. Adding a cascaded streaming replica
    14. Getting ready...
    15. How to do it ...
    16. How it works...
    17. Promoting a standby in a replication cluster with multiple standby servers
    18. Getting ready...
    19. How to do it...
    20. How it works...
    21. Using pg_rewind to re-synchronize a demoted master
    22. Getting ready...
    23. How to do it...
    24. How it works...
    25. Enabling synchronous streaming replication
    26. Getting ready...
    27. How to do it...
    28. How it works...
    29. Setting up logical replication in PostgreSQL 13
    30. Getting ready...
    31. How to do it...
    32. How it works...
  9. High Availability and Automatic Failover
    1. Technical requirements
    2. Automatic failover using Patroni
    3. Enabling distributed consensus using etcd
    4. Getting ready
    5. How to do it...
    6. How it works...
    7. Avoiding split-brain using Watchdog/softdog
    8. Getting ready
    9. How to do it...
    10. How it works...
    11. Installing Patroni along with its Python dependencies
    12. Getting ready...
    13. How to do it...
    14. How it works...
    15. Creating a Patroni configuration file
    16. Getting ready...
    17. How to do it...
    18. How it works...
    19. Starting Patroni as a service using systemd
    20. Getting ready...
    21. How to do it...
    22. How it works...
    23. Initializing a PostgreSQL primary database using Patroni
    24. Getting ready...
    25. How to do it...
    26. How it works...
    27. Adding a standby to a Patroni cluster
    28. Getting ready...
    29. How to do it...
    30. How it works...
    31. Performing a manual switchover using Patroni
    32. Getting ready...
    33. How to do it...
    34. How it works...
  10. Connection Pooling and Load Balancing
    1. Technical requirements
    2. Installing pgBouncer on a Linux server
    3. Getting ready...
    4. How to do it...
    5. How it works...
    6. Creating a pgBouncer configuration file
    7. Getting ready...
    8. How to do it...
    9. How it works...
    10. Configuring the pool settings on pgBouncer
    11. Getting ready...
    12. How to do it...
    13. How it works...
    14. Starting and stopping the pgBouncer service
    15. Getting ready...
    16. How to do it...
    17. How it works...
    18. Installing HAProxy on Linux servers
    19. Getting ready...
    20. How to do it...
    21. How it works...
    22. Using xinetd to detect a primary or a standby
    23. Getting ready...
    24. How to do it...
    25. How it works...
    26. Creating an HAProxy configuration file
    27. Getting ready...
    28. How to do it...
    29. How it works...
    30. Starting and stopping the HAProxy service
    31. Getting ready...
    32. How to do it...
    33. How it works...
    34. Building a robust HA cluster using Patroni, pgBouncer, and HAProxy
    35. Getting ready...
    36. How to do it...
    37. How it works...
  11. Securing through Authentication
    1. Technical requirements
    2. Securing client connections using the pg_hba.conf file
    3. Categories in the pg_hba.conf file
    4. Getting ready
    5. How to do it...
    6. How it works...
    7. Performing authorization using roles and privileges
    8. Getting ready
    9. How to do it...
    10. How it works...
    11. Setting up row-level security
    12. Getting ready
    13. How to do it...
    14. How it works...
    15. Configuring encryption of data over the wire using SSL
    16. Getting ready
    17. How to do it...
    18. How it works...
    19. Enabling certificate authentication using SSL
    20. Getting ready
    21. How to do it...
    22. How it works...
    23. Auditing PostgreSQL through logging
    24. Getting ready
    25. How to do it...
    26. How it works...
    27. Auditing PostgreSQL using pgaudit
    28. Getting ready
    29. How to do it...
    30. How it works...
    31. Setting up object-level auditing using pgaudit
    32. Getting ready
    33. How to do it...
    34. How it works...
  12. Logging and Analyzing PostgreSQL Servers
    1. Technical requirements
    2. Setting up slow query logging in PostgreSQL
    3. Getting ready
    4. How to do it...
    5. How it works...
    6. There's more...
    7. Logging runtime execution plans in PostgreSQL using auto_explain
    8. Getting ready
    9. How to do it...
    10. Global level
    11. Session level
    12. How it works...
    13. Logging locks, waits, and temp in PostgreSQL
    14. Getting ready
    15. How to do it...
    16. How it works...
    17. Logging autovacuum and analyzing activity in PostgreSQL
    18. Getting ready
    19. How to do it...
    20. How it works...
    21. Generating a pgBadger report
    22. Getting ready
    23. How to do it...
    24. How it works...
    25. Configuring pg_stat_statements as an extension
    26. Getting ready
    27. How to do it...
    28. How it works...
    29. Query analysis using pg_stat_statements
    30. Getting ready
    31. How to do it...
    32. How it works...
    33. Getting the kernel-level statistics of a query using pg_stat_kcache
    34. Getting ready
    35. How to do it...
    36. How it works...
  13. Critical Services Monitoring
    1. Technical requirements
    2. Installation of Grafana and its dependencies
    3. Getting ready
    4. How to do it...
    5. How it works... 
    6. Prometheus as a data source on the monitoring server
    7. Getting ready
    8. How to do it...
    9. How it works...
    10. Configuring Node Exporter on Postgres servers to monitor operating system metrics
    11. Getting ready
    12. How to do it...
    13. How it works...
    14. Adding metrics being collected using node_exporter to Prometheus
    15. Getting ready
    16. How to do it...
    17. How it works...
    18. Collecting PostgreSQL metrics using postgres_exporter
    19. Getting ready
    20. How to do it...
    21. How it works...
    22. Adding metrics exposed by postgres_exporter to Prometheus
    23. Getting ready
    24. How to do it...
    25. How it works...
    26. Importing a dashboard for monitoring Linux metrics
    27. Getting ready
    28. How to do it...
    29. How it works...
    30. How to import a dashboard for monitoring Postgres metrics
    31. Getting ready
    32. How to do it...
    33. How it works...
    34. Adding custom queries to postgres_exporter
    35. Getting ready
    36. How to do it...
    37. How it works...
  14. Extensions and Performance Tuning
    1. Technical requirements
    2. Installing and creating pg_repack to rebuild objects online
    3. Getting ready
    4. How to do it...
    5. Installing pg_repack on CentOS
    6. Installing pg_repack on Ubuntu
    7. How it works...
    8. How to rebuild a table online using pg_repack
    9. Getting ready
    10. How to do it...
    11. How it works...
    12. How to rebuild indexes of a table online using pg_repack
    13. Getting ready
    14. How to do it...
    15. Rebuilding all the indexes of a table
    16. Rebuilding a specific index
    17. How it works...
    18. Moving a table or an index to another tablespace online
    19. Getting ready
    20. How to do it...
    21. How it works...
    22. Warming up the cache using pg_prewarm
    23. Getting ready
    24. How to do it...
    25. How it works...
    26. How to tune a function or a stored procedure using plprofiler
    27. Getting ready
    28. How to do it...
    29. How it works...
    30. Capturing statements that require tuning using pg_stat_statements
    31. Getting ready
    32. How to do it...
    33. How it works...
    34. Viewing the execution plans using EXPLAIN in PostgreSQL
    35. Getting ready
    36. How to do it...
    37. How it works...
  15. Upgrades and Patches
    1. Technical requirements
    2. Finding the difference between a major and minor release in PostgreSQL
    3. What is an obsolete version?
    4. Technical requirements
    5. Major version upgrade to PostgreSQL 13 using pg_dumpall
    6. Getting ready
    7. How to do it...
    8. How it works...
    9. Major version upgrade to PostgreSQL 13 using pg_dump and pg_restore
    10. Getting ready
    11. How to do it...
    12. How it works...
    13. Major version upgrade to PostgreSQL 13 using pg_upgrade with downtime
    14. Getting ready
    15. How to do it...
    16. How it works...
    17. Major version upgrade to PostgreSQL 13 using pg_upgrade with hard links for seamless downtime
    18. Getting ready
    19. How to do it...
    20. How it works...
    21. Installing the pglogical extension to upgrade older versions to PostgreSQL 13
    22. Getting ready
    23. How to do it...
    24. How it works...
    25. Upgrading to PostgreSQL 13 using the pglogical extension
    26. Getting ready
    27. How to do it...
    28. How it works...
    29. Upgrading to PostgreSQL 13 using logical replication and logical decoding
    30. Getting ready
    31. How to do it...
    32. How it works...
    33. Updating the minor version of PostgreSQL 13
    34. Getting ready
    35. How to do it...
    36. How it works...
  16. About Packt
    1. Why subscribe?
  17. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: PostgreSQL 13 Cookbook
  • Author(s): Vallarapu Naga Avinash Kumar
  • Release date: February 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781838648138