Reader small image

You're reading from  Learn PostgreSQL - Second Edition

Product typeBook
Published inOct 2023
PublisherPackt
ISBN-139781837635641
Edition2nd Edition
Right arrow
Authors (2):
Luca Ferrari
Luca Ferrari
author image
Luca Ferrari

Luca Ferrari has been passionate about computer science since the Commodore 64 era, and today holds a master's degree (with honors) and a Ph.D. from the University of Modena and Reggio Emilia. He has written several research papers, technical articles, and book chapters. In 2011, he was named an Adjunct Professor by Nipissing University. An avid Unix user, he is a strong advocate of open source, and in his free time, he collaborates with a few projects. He met PostgreSQL back in release 7.3; he was a founder and former president of the Italian PostgreSQL Community (ITPUG). He also talks regularly at technical conferences and events and delivers professional training.
Read more about Luca Ferrari

Enrico Pirozzi
Enrico Pirozzi
author image
Enrico Pirozzi

Enrico Pirozzi, EnterpriseDB certified on implementation management and tuning, with a master's in computer science, has been a PostgreSQL DBA since 2003. Based in Italy, he has been providing database advice to clients in industries such as manufacturing and web development for 10 years. He has been training others on PostgreSQL since 2008. Dedicated to open source technology since early in his career, he is a cofounder of the PostgreSQL Italian mailing list, PostgreSQL-it, and of the PostgreSQL Italian community site, PSQL
Read more about Enrico Pirozzi

View More author details
Right arrow

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 leader. Both asynchronous and synchronous replication, as well as replication slots and delayed replication, will be presented.

Chapter 18, Logical Replication, covers logical replication, which allows very fine-grained replication specifying which tables have to be replicated and which don’t – supported by PostgreSQL since version 10. This, of course, allows a very new and rich scenario of data sharing across different database instances. The chapter presents how logical replication works, how to set it up, and how to monitor the replication.

Chapter 19, Useful Tools and Useful Extensions, should be considered as an appendix to the book. In this chapter, we will talk about some tools and extensions that allow a database administrator to maximize work done while minimizing effort.

To get the most out of this book

For this book to be useful, basic knowledge of the Linux (or another Unix-like) operating system is required. All the SQL examples can be run using the psql command-line program or any available GUI tool (not presented in the book), like the PostgreSQL-specific pgAdmin4. Shell scripts will be executed using the GNU Bash scripting language.

Software/hardware covered in the book

OS requirements

PostgreSQL 16

Linux OS/Unix-like OS (e.g., FreeBSD, OpenBSD)

The book provides a set of Docker images, so that the reader can follow and test all the code examples. Running the Docker images is not mandatory, but it does not require you to have your own customized PostgreSQL installation. In order to run the Docker images, you need to install the Docker application on your operating system.

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Learn-PostgreSQL-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781837635641.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system.”

A block of code is set as follows:

SELECT rolname, rolcanlogin,
            rolconnlimit, rolpassword
            FROM pg_roles
            WHERE rolname = 'luca';

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

SELECT line_number, type,
                  database, user_name,
                  address, auth_method
                  FROM pg_hba_file_rules;

Any command-line input or output is written as follows:

$ sudo cat $PGDATA/rejected_users.txt

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “Select System info from the Administration panel.”

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email feedback@packtpub.com and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you’ve read Learn PostgreSQL, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. 

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781837635641

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn PostgreSQL - Second Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781837635641
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Luca Ferrari

Luca Ferrari has been passionate about computer science since the Commodore 64 era, and today holds a master's degree (with honors) and a Ph.D. from the University of Modena and Reggio Emilia. He has written several research papers, technical articles, and book chapters. In 2011, he was named an Adjunct Professor by Nipissing University. An avid Unix user, he is a strong advocate of open source, and in his free time, he collaborates with a few projects. He met PostgreSQL back in release 7.3; he was a founder and former president of the Italian PostgreSQL Community (ITPUG). He also talks regularly at technical conferences and events and delivers professional training.
Read more about Luca Ferrari

author image
Enrico Pirozzi

Enrico Pirozzi, EnterpriseDB certified on implementation management and tuning, with a master's in computer science, has been a PostgreSQL DBA since 2003. Based in Italy, he has been providing database advice to clients in industries such as manufacturing and web development for 10 years. He has been training others on PostgreSQL since 2008. Dedicated to open source technology since early in his career, he is a cofounder of the PostgreSQL Italian mailing list, PostgreSQL-it, and of the PostgreSQL Italian community site, PSQL
Read more about Enrico Pirozzi