PostgreSQL 14 Administration Cookbook

Book description

Administer, monitor, and replicate your PostgreSQL 14 database for efficient database management and maintenance

Key Features

  • Troubleshoot and tackle any administration and management problems in PostgreSQL 14
  • Find expert techniques for monitoring, fine-tuning, and securing your database
  • Adopt efficient replication for high availability with PostgreSQL 14

Book Description

PostgreSQL is a powerful, open-source database management system with an enviable reputation for high performance and stability. With many new features in its arsenal, PostgreSQL 14 allows you to scale up your PostgreSQL infrastructure. With this book, you'll take a step-by-step, recipe-based approach to effective PostgreSQL administration.

This book will get you up and running with all the latest features of PostgreSQL 14 while helping you explore the entire database ecosystem. You'll learn how to tackle a variety of problems and pain points you may face as a database administrator such as creating tables, managing views, improving performance, and securing your database. As you make progress, the book will draw attention to important topics such as monitoring roles, validating backups, regular maintenance, and recovery of your PostgreSQL 14 database. This will help you understand roles, ensuring high availability, concurrency, and replication. Along with updated recipes, this book touches upon important areas like using generated columns, TOAST compression, PostgreSQL on the cloud, and much more.

By the end of this PostgreSQL book, you'll have gained the knowledge you need to manage your PostgreSQL 14 database efficiently, both in the cloud and on-premise.

What you will learn

  • Plan, manage, and maintain PostgreSQL databases in production
  • Work with the newly introduced features of PostgreSQL 14
  • Use pgAdmin or OmniDB to perform database administrator (DBA) tasks
  • Use psql to write accurate and repeatable scripts
  • Understand how to tackle real-world data issues with the help of examples
  • Select and implement robust backup and recovery techniques in PostgreSQL 14
  • Deploy best practices for planning and designing live databases

Who this book is for

This Postgres 14 book is for database administrators, data architects, database developers, and anyone with an interest in planning and running live production databases using PostgreSQL 14. Those looking for hands-on solutions to any problem associated with PostgreSQL 14 administration will also find this book useful. Some experience with handling PostgreSQL databases will help you to make the most out of this book, however, it is a useful resource even if you are just beginning your Postgres journey.

Table of contents

  1. PostgreSQL 14 Administration Cookbook
  2. Contributors
  3. About the authors
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
    4. Download the color images
    5. Conventions used
    6. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    7. Get in touch
    8. Reviews
    9. Share Your Thoughts
  6. Chapter 1: First Steps
    1. Introducing PostgreSQL 14
      1. What makes PostgreSQL different?
    2. How to get PostgreSQL
      1. How to do it...
      2. How it works...
      3. There's more…
    3. Connecting to the PostgreSQL server
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Enabling access for network/remote users
      1. How to do it…
      2. How it works…
      3. There's more…
      4. See also
    5. Using the pgAdmin4 GUI tool
      1. How to do it…
      2. How it works…
    6. Using the OmniDB GUI tool
      1. How to do it…
      2. See also
    7. Using the psql query and scripting tool
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    8. Changing your password securely
      1. How to do it…
      2. How it works…
    9. Avoiding hardcoding your password
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    10. Using a connection service file
      1. How to do it…
      2. How it works…
    11. Troubleshooting a failed connection
      1. How to do it…
      2. There's more…
    12. PostgreSQL in the cloud
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  7. Chapter 2: Exploring the Database
    1. What type of server is this?
      1. How to do it…
      2. There's more...
    2. What version is the server?
      1. How to do it…
      2. How it works…
      3. There's more…
    3. What is the server uptime?
      1. How to do it…
      2. How it works…
      3. See also
    4. Locating the database server files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more…
    5. Locating the database server's message log
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Locating the database's system identifier
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Listing databases on the database server
      1. How to do it…
      2. How it works...
      3. There's more...
    8. How many tables are there in a database?
      1. How to do it...
      2. How it works…
      3. There's more…
    9. How much disk space does a database use?
      1. How to do it...
      2. How it works...
    10. How much disk space does a table use?
      1. How to do it…
      2. How it works…
      3. There's more…
    11. Which are my biggest tables?
      1. How to do it...
      2. How it works…
    12. How many rows are there in a table?
      1. How to do it…
      2. How it works...
    13. Quickly estimating the number of rows in a table
      1. How to do it…
      2. How it works…
      3. There's more…
    14. Listing extensions in this database
      1. How to do it…
      2. How it works…
      3. There's more…
      4. See also
    15. Understanding object dependencies
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  8. Chapter 3: Server Configuration
    1. RTFM
      1. How to do it…
      2. How it works…
      3. There's more…
    2. Planning a new database
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Setting configuration parameters for the database server
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Setting configuration parameters in your programs
      1. How to do it…
      2. How it works…
      3. There's more…
    5. Finding the configuration settings for your session
      1. How to do it…
      2. How it works…
    6. Finding parameters with non-default settings
      1. How to do it…
      2. How it works...
      3. There's more...
    7. Setting parameters for particular groups of users
      1. How to do it…
      2. How it works…
    8. A basic server configuration checklist
      1. Getting ready
      2. How to do it…
      3. There's more…
    9. Adding an external module to PostgreSQL
      1. Getting ready
      2. How to do it…
      3. How it works...
    10. Using an installed module/extension
      1. Getting ready
      2. How to do it…
      3. How it works...
    11. Managing installed extensions
      1. How to do it…
      2. How it works…
      3. There's more…
  9. Chapter 4: Server Control
    1. Overview of controlling the database server
    2. Starting the database server manually
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Stopping the server safely and quickly
      1. How to do it…
      2. How it works…
      3. See also
    4. Stopping the server in an emergency
      1. How to do it…
      2. How it works…
    5. Reloading the server configuration files
      1. How to do it…
      2. How it works…
      3. There's more…
    6. Restarting the server quickly
      1. How to do it…
      2. There's more…
    7. Preventing new connections
      1. How to do it…
      2. How it works…
    8. Restricting users to only one session each
      1. How to do it…
      2. How it works…
    9. Pushing users off the system
      1. How to do it…
      2. How it works…
    10. Deciding on a design for multitenancy
      1. How to do it…
      2. How it works…
    11. Using multiple schemas
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Giving users their own private databases
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    13. Running multiple servers on one system
      1. Getting ready
      2. How to do it…
      3. How it works…
    14. Setting up a connection pool
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    15. Accessing multiple servers using the same host and port
      1. Getting ready
      2. How to do it…
      3. There's more…
  10. Chapter 5: Tables and Data
    1. Choosing good names for database objects
      1. Getting ready
      2. How to do it…
      3. There's more…
    2. Handling objects with quoted names
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There's more…
    3. Enforcing the same name and definition for columns
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There's more…
    4. Identifying and removing duplicates
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Preventing duplicate rows
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    6. Finding a unique key for a set of data
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Generating test data
      1. How to do it...
      2. How it works…
      3. There's more…
      4. See also
    8. Randomly sampling data
      1. How to do it…
      2. How it works...
    9. Loading data from a spreadsheet
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Loading data from flat files
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There's more…
    11. Making bulk data changes using server-side procedures with transactions
      1. Getting ready
      2. How to do it…
      3. There's more…
  11. Chapter 6: Security
    1. Overview of PostgreSQL security
      1. Typical user roles
    2. The PostgreSQL superuser
      1. How to do it…
      2. How it works…
      3. There's more…
      4. See also
    3. Revoking user access to a table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Granting user access to a table
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more…
    5. Granting user access to specific columns
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Granting user access to specific rows
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    7. Creating a new user
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There's more…
    8. Temporarily preventing a user from connecting
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more…
    9. Removing a user without dropping their data
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Checking whether all users have a secure password
      1. How to do it…
      2. How it works…
    11. Giving limited superuser powers to specific users
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    12. Auditing database access
      1. Getting ready
      2. Auditing access
      3. Auditing SQL
      4. Auditing table access
      5. Managing the audit log
      6. Auditing data changes
    13. Always knowing which user is logged in
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    14. Integrating with LDAP
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    15. Connecting using encryption (SSL/GSSAPI)
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    16. Using SSL certificates to authenticate
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    17. Mapping external usernames to database roles
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    18. Using column-level encryption
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    19. Setting up cloud security using predefined roles
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  12. Chapter 7: Database Administration
    1. Writing a script that either succeeds entirely or fails entirely
      1. How to do it…
      2. How it works…
      3. There's more…
    2. Writing a psql script that exits on the first error
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Using psql variables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Placing query output into psql variables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Writing a conditional psql script
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Investigating a psql error
      1. Getting ready
      2. How to do it…
      3. There's more…
    7. Setting the psql prompt with useful information
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Using pgAdmin for DBA tasks
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more…
    9. Scheduling jobs for regular background execution
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    10. Performing actions on many tables
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    11. Adding/removing columns on a table
      1. How to do it…
      2. How it works…
      3. There's more…
    12. Changing the data type of a column
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    13. Changing the definition of an enum data type
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    14. Adding a constraint concurrently
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    15. Adding/removing schemas
      1. How to do it…
      2. There's more…
    16. Moving objects between schemas
      1. How to do it…
      2. How it works…
      3. There's more…
    17. Adding/removing tablespaces
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    18. Moving objects between tablespaces
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    19. Accessing objects in other PostgreSQL databases
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    20. Accessing objects in other foreign databases
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    21. Making views updatable
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    22. Using materialized views
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    23. Using GENERATED data columns
      1. How to do it…
      2. How it works…
      3. There's more…
    24. Using data compression
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  13. Chapter 8: Monitoring and Diagnosis
    1. Overview of PostgreSQL monitoring
    2. Cloud-native monitoring
    3. Providing PostgreSQL information to monitoring tools
      1. Finding more information about generic monitoring tools
    4. Real-time viewing using pgAdmin
      1. Getting ready
      2. How to do it…
    5. Checking whether a user is connected
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Checking whether a computer is connected
      1. How to do it…
      2. There's more…
    7. Repeatedly executing a query in psql
      1. How to do it…
      2. There's more…
    8. Checking which queries are running
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    9. Monitoring the progress of commands
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    10. Checking which queries are active or blocked
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    11. Knowing who is blocking a query
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Killing a specific session
      1. How to do it…
      2. How it works…
      3. There's more…
    13. Detecting an in-doubt prepared transaction
      1. How to do it…
    14. Knowing whether anybody is using a specific table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    15. Knowing when a table was last used
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more…
    16. Usage of disk space by temporary data
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    17. Understanding why queries slow down
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    18. Analyzing the real-time performance of your queries
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    19. Investigating and reporting a bug
      1. Getting ready
      2. How to do it…
      3. How it works…
  14. Chapter 9: Regular Maintenance
    1. Controlling automatic database maintenance
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    2. Avoiding auto-freezing and page corruptions
      1. How to do it…
    3. Removing issues that cause bloat
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Removing old prepared transactions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Actions for heavy users of temporary tables
      1. How to do it…
      2. How it works…
    6. Identifying and fixing bloated tables and indexes
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Monitoring and tuning a vacuum
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. Maintaining indexes
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    9. Finding unused indexes
      1. How to do it…
      2. How it works…
    10. Carefully removing unwanted indexes
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. Planning maintenance
      1. How to do it…
      2. How it works…
      3. There's more…
  15. Chapter 10: Performance and Concurrency
    1. Finding slow SQL statements
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    2. Finding out what makes SQL slow
      1. Getting ready
      2. How to do it…
      3. There's more…
      4. See also
    3. Reducing the number of rows returned
      1. How to do it…
      2. There's more…
    4. Simplifying complex SQL queries
      1. Getting ready
      2. How to do it…
      3. There's more…
    5. Speeding up queries without rewriting them
      1. How to do it…
      2. There's more…
    6. Discovering why a query is not using an index
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Forcing a query to use an index
      1. Getting ready
      2. How to do it…
      3. There's more…
    8. Using parallel query
      1. How to do it…
      2. How it works…
    9. Creating time-series tables using partitioning
      1. How to do it…
      2. How it works…
      3. There's more…
    10. Using optimistic locking to avoid long lock waits
      1. How to do it…
      2. How it works…
      3. There's more…
    11. Reporting performance problems
      1. How to do it…
      2. There's more…
  16. Chapter 11: Backup and Recovery
    1. Understanding and controlling crash recovery
      1. How to do it…
      2. How it works…
      3. There's more…
    2. Planning your backups
      1. How to do it…
      2. There's more…
    3. Hot logical backups of one database
      1. How to do it…
      2. How it works…
      3. There's more…
      4. See also
    4. Hot logical backups of all databases
      1. How to do it…
      2. How it works…
      3. See also
    5. Backups of database object definitions
      1. How to do it…
      2. There's more…
    6. A standalone hot physical backup
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Hot physical backups with Barman
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. Recovery of all databases
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    9. Recovery to a point in time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    10. Recovery of a dropped/damaged table
      1. How to do it…
      2. How it works…
      3. See also
    11. Recovery of a dropped/damaged database
      1. How to do it…
    12. Extracting a logical backup from a physical one
      1. Getting ready
      2. How to do it…
      3. There's more…
    13. Improving performance of logical backup/recovery
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    14. Improving performance of physical backup/recovery
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There's more…
      5. See also
    15. Validating backups
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  17. Chapter 12: Replication and Upgrades
    1. Replication concepts
      1. Topics
      2. Basic concepts
      3. History and scope
      4. Practical aspects
      5. Data loss
      6. Single-master replication
      7. Multinode architectures
      8. Multi-master replication
      9. Other approaches to replication
    2. Replication best practices
      1. Getting ready
      2. How to do it…
      3. There's more…
    3. Setting up streaming replication
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. Setting up streaming replication security
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Hot Standby and read scalability
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Managing streaming replication
      1. Getting ready
      2. How to do it…
      3. There's more…
      4. See also
    7. Using repmgr
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. Using replication slots
      1. Getting ready
      2. How to do it…
      3. There's more…
      4. See also
    9. Monitoring replication
      1. Getting ready
      2. How to do it…
      3. There's more…
    10. Performance and sync rep
      1. Getting ready
      2. How to do it...
      3. How it works…
      4. There's more…
    11. Delaying, pausing, and synchronizing replication
      1. Getting ready
      2. How to do it…
      3. There's more…
      4. See also
    12. Logical replication
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    13. BDR
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more…
    14. Archiving transaction log data
      1. Getting ready
      2. How to do it…
      3. There's more...
      4. See also
    15. Upgrading minor releases
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    16. Major upgrades in-place
      1. Getting ready
      2. How to do it…
      3. How it works…
    17. Major upgrades online
      1. How to do it…
      2. How it works...
    18. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: PostgreSQL 14 Administration Cookbook
  • Author(s): Simon Riggs, Gianni Ciolli
  • Release date: March 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803248974