PostgreSQL 10 Administration Cookbook Over 165 effective recipes for database management and maintenance in PostgreSQL 10 Simon Riggs Gianni Ciolli BIRMINGHAM - MUMBAI PostgreSQL 10 Administration Cookbook Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Amey Varangaonkar Acquisition Editor: Namrata Patil Content Development Editor: Amrita Noronha Technical Editor: Sneha Hanchate Copy Editor: Safis Project Coordinator: Shweta Birwatkar Proofreader: Safis Editing Indexer: Pratik Shirodkar Graphics: Jisha Chirayil Production Coordinator: Shraddha Falebhai First published: April 2017 Production reference: 1070518 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78847-492-4 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the authors Simon Riggs is the CTO of 2ndQuadrant, having contributed to PostgreSQL as a major developer and committer for 14 years. He has written and designed features for replication, performance, BI, management, and security. Under his guidance, 2ndQuadrant is now a leading developer of open source PostgreSQL, serving hundreds of clients in USA, Europe, and worldwide. Simon is a frequent speaker at many conferences on PostgreSQL Futures. He has worked as a database architect for 30 years. Gianni Ciolli is the head of professional services at 2ndQuadrant. PostgreSQL consultant, trainer, and speaker at many PostgreSQL conferences in Europe and abroad since the last 10 years. He has a PhD in Mathematics from the University of Florence. He has worked with free and open source software since the 1990s and is active in the community (Prato Linux User Group and Italian PostgreSQL Users Group). He lives in London with his son. His other interests include music, drama, poetry, and athletics. About the reviewer Sheldon Strauch is a 23-year veteran of software consulting at IBM, Sears, Ernst & Young, and Kraft Foods. Having a bachelor's degree in business administration, he leverages his technical skills to improve business' self-awareness. He is interested in data gathering, management, and mining; maps and mapping; business intelligence; and application of data analysis. He is currently focusing on the development of data management and mining at Enova International at Chicago. At his leisure, he enjoys performing arts, music, and traveling with his wife, Marilyn. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: First Steps 9 Introduction 9 Introducing PostgreSQL 10 9 What makes PostgreSQL different? 10 Robustness 12 Security 12 Ease of use 13 Extensibility 13 Performance and concurrency 14 Scalability 14 SQL and NoSQL 14 Popularity 15 Commercial support 15 Research and development funding 16 Getting PostgreSQL 16 How to do it... 16 How it works... 17 There's more… 17 Connecting to the PostgreSQL server 18 Getting ready 18 How to do it… 18 How it works… 20 There's more… 21 See also 21 Enabling access for network/remote users 21 How to do it… 22 How it works… 22 There's more… 24 See also 24 Using graphical administration tools 24 How to do it… 24 How it works… 27 OmniDB 28 See also 30 Using the psql query and scripting tool 31 Getting ready 31 How to do it… 31 How it works… 33 There's more… 34 Table of Contents See also 35 Changing your password securely 35 How to do it… 35 How it works… 35 Avoiding hardcoding your password 36 Getting ready 36 How to do it… 37 How it works… 37 There's more… 38 Using a connection service file 38 How to do it… 39 How it works… 39 Troubleshooting a failed connection 39 How to do it… 40 There's more… 41 Chapter 2: Exploring the Database 42 Introduction 42 What version is the server? 43 How to do it… 43 How it works… 43 There's more… 44 What is the server uptime? 45 How to do it… 45 How it works... 45 See also 46 Locating the database server files 46 Getting ready 46 How to do it... 46 How it works... 48 There's more… 49 Locating the database server's message log 50 Getting ready 51 How to do it... 51 How it works... 52 There's more... 53 Locating the database's system identifier 53 Getting ready 53 How to do it… 54 How it works… 54 Listing databases on this database server 56 How to do it… 56 How it works... 57 There's more... 58 How many tables are there in a database? 59 [ ii ] Table of Contents How to do it... 59 How it works… 60 There's more… 61 How much disk space does a database use? 62 How to do it... 62 How it works... 62 How much disk space does a table use? 62 How to do it… 63 How it works… 63 There's more… 64 Which are my biggest tables? 64 How to do it... 64 How it works… 65 How many rows are there in a table? 65 How to do it… 65 How it works... 66 Quickly estimating the number of rows in a table 67 How to do it… 67 How it works… 68 There's more… 68 Function 1 – Estimating the number of rows 70 Function 2 – Computing the size of a table without locks 70 Listing extensions in this database 72 Getting ready 73 How to do it… 73 How it works… 73 There's more… 74 Understanding object dependencies 74 Getting ready 74 How to do it… 75 How it works… 75 There's more… 75 Chapter 3: Configuration 77 Introduction 77 Reading the fine manual 78 How to do it… 78 How it works… 79 There's more… 79 Planning a new database 79 Getting ready 79 How to do it… 80 How it works… 80 There's more… 81 Changing parameters in your programs 81 [ iii ] Table of Contents How to do it… 81 How it works… 82 There's more… 82 Finding the current configuration settings 83 How to do it… 83 There's more… 85 How it works… 85 Which parameters are at non-default settings? 85 How to do it… 86 How it works... 87 There's more... 87 Updating the parameter file 87 Getting ready 88 How to do it… 88 How it works… 89 There's more… 89 Setting parameters for particular groups of users 90 How to do it… 90 How it works… 91 The basic server configuration checklist 91 Getting ready 91 How to do it… 92 There's more… 92 Adding an external module to PostgreSQL 93 Getting ready 94 How to do it… 95 Installing modules using a software installer 95 Installing modules from PGXN 96 Installing modules from a manually downloaded package 97 Installing modules from source code 97 How it works... 98 Using an installed module 98 Getting ready 99 How to do it… 99 How it works... 100 Managing installed extensions 100 How to do it… 100 How it works… 102 There's more… 103 Chapter 4: Server Control 104 Introduction 104 Starting the database server manually 105 Getting ready 106 How to do it… 106 [ iv ]