All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted, in any form without prior written permission of the author.
Every effort has been made to ensure the accuracy of the information presented. However, the information contained in the book is sold without warranty, either express or implied. The author will not be held liable for any damages caused directly or indirectly by this book.
To Patti, Jacki, and R j - the best family in the history of families.
Introduction
PostgreSQL is a popular free and open source relational database management system. It is also widely known as Postgres. I will refer to it by its more informal alias, Postgres, in this book rather than as PostgreSQL because thats the way I hear it said most often in the real world.
A relational database management system (RDMS) or relational database, at its heart, is a system to store and retrieve data from a database. The term relation describes a structure that stores data in a grid of rows and columns. A relation is essentially a database table, so a relational database is a database system that stores data in tables that have rows and columns.
Structured Query Language (SQL) is the language we use to interact with relational databases, including Postgres. SQL allows us to store, modify, and delete data from our database. It also allows us to create and remove tables, query our data, and much more.
Postgres is a great technology to learn. It is a powerful, secure, and fast relational database system that is growing in popularity. It is a mature, stable database that has been around for over 30 years. Postgres runs on a wide variety of operating systems, including Linux, Windows, macOS, and it even runs in the cloud. Postgres helps you to maintain the integrity of your data. It has a robust set of features, and is extensible if you ever need to do something unconventional that it doesnt already do.
This book, Essential Postgres , will give you exposure to the most used and useful parts of Postgres development, as well as tips and insights that I have picked up over my years of working with Postgres. Writing SQL statements, creating tables, functions, triggers, views, and data integrity are explained.
Audience
This book is suitable for anybody who wants to learn to use Postgres. That includes folks who are new to Postgres and databases, intermediate-level developers who would like a refresher, and even seasoned software developers who are transitioning from another database to Postgres. This book is for anybody who is interested in learning about the essentials of using the Postgres database.
The aim of this book is to show you how to do things with the Postgres database. The book is short on theory and long on examples. If you are the type of person who likes that sort of thing, read on.
Since this book focuses on Postgres development rather than administration , if you are a Postgres Database Administrator (DBA) you may want to choose another book. This book does not discuss backup, recovery, security or other DBA-related issues.
Conventions
In this book, I have made some stylistic choices that you might want to be aware of.
Ellipses Used in Examples
There are several examples in this book that show data being returned by the Postgres database. In cases where many rows are returned, I have shown a few of the rows and added an ellipsis () rather than showing all the results and making the book 900 pages long. When you see be aware that I am not showing all of the results in order to save space. For example:
president_id | president_name | president_party |
| George Washington | null |
| John Adams | Federalist |
| Thomas Jefferson | Democratic-Republican |
| Barack Obama | Democrat |
| Donald Trump | Republican |
| Joe Biden | Democrat |
In this case, a full set of all 46 presidents was returned from the database, but I omitted presidents 4 43 for the sake of brevity.
Parens
I often refer to parentheses as parens because thats the way I most often hear them referred to among developers.
Also, I add parens to the end of function names. For example, Postgres has a function called upper that I write as upper ( ) . You would say this aloud as the upper function, but in this book I add the parens to the end of function name to make it clear that upper( ) is a function. Functions are explained later in this book, and well see what the parens are for then.
1
Selecting Data from a Postgres Database
Introducing Database Tables
In Postgres, data is stored in database tables. Think of a table as a grid of rows and columns where data can be saved. You can create your own tables and choose the names of the columns that you want, and specify what type of data they should contain. Each record is saved as a row in the table.
For example, here is a table called us_president that has been defined with three columns: president_id, president_name, and president_party.
president_id | president_name | president_party |
| George Washington | null |
| John Adams | Federalist |
| Thomas Jefferson | Democratic-Republican |
| James Madison | Democratic-Republican |
| James Monroe | Democratic-Republican |
| John Quincy Adams | Democratic-Republican |
| Andrew Jackson | Democrat |
| Martin Van Buren | Democrat |
| William Harrison | Whig |
| John Tyler | Whig |
| James Polk | Democrat |
| Zachary Taylor | Whig |
| Millard Fillmore | Whig |
| Franklin Pierce | Democrat |
| James Buchanan | Democrat |
| Abraham Lincoln | Republican |
| Andrew Johnson | Democrat |
| Ulysses Grant | Republican |
| Rutherford Hayes | Republican |
| James Abram Garfield | Republican |
| Chester Arthur | Republican |
| Grover Cleveland | Democrat |
| Benjamin Harrison | Republican |
| Grover Cleveland | Democrat |
| William McKinley | Republican |