848 PostgreSQL up and running

164 145 0
848 PostgreSQL  up and running

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info www.it-ebooks.info PostgreSQL: Up and Running Regina Obe and Leo Hsu Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo www.it-ebooks.info PostgreSQL: Up and Running by Regina Obe and Leo Hsu Copyright © 2012 Regina Obe and Leo Hsu All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://my.safaribooksonline.com) For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editor: Meghan Blanchette Production Editor: Iris Febres Proofreader: Iris Febres Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Rebecca Demarest Revision History for the First Edition: 2012-07-02 First release See http://oreilly.com/catalog/errata.csp?isbn=9781449326333 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc PostgreSQL: Up and Running, the image of the elephant shrew, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-1-449-32633-3 [LSI] 1341247831 www.it-ebooks.info Table of Contents Preface ix The Basics Where to Get PostgreSQL Notable PostgreSQL Forks Administration Tools What’s New in Latest Versions of PostgreSQL? Why Upgrade? What to Look for in PostgreSQL 9.2 PostgreSQL 9.1 Improvements Database Drivers Server and Database Objects Where to Get Help 1 4 5 Database Administration Configuration Files The postgresql.conf File The pg_hba.conf File Reload the Configuration Files Setting Up Groups and Login Roles (Users) Creating an Account That Can Log In Creating Group Roles Roles Inheriting Rights Databases and Management Creating and Using a Template Database Organizing Your Database Using Schemas Permissions Extensions and Contribs Installing Extensions Common Extensions Backup 10 12 14 14 15 15 15 16 16 16 17 18 19 21 22 iii www.it-ebooks.info Selective Backup Using pg_dump Systemwide Backup Using pg_dumpall Restore Terminating Connections Using psql to Restore Plain Text SQL backups Using pg_restore Managing Disk Space with Tablespaces Creating Tablespaces Moving Objects Between Tablespaces Verboten Delete PostgreSQL Core System Files and Binaries Giving Full Administrative Rights to the Postgres System (Daemon) Account Setting shared_buffers Too High Trying to Start PostgreSQL on a Port Already in Use 23 24 24 24 25 26 27 27 27 27 28 28 29 29 psql 31 Interactive psql Non-Interactive psql Session Configurations Changing Prompts Timing Details AUTOCOMMIT Shortcuts Retrieving Prior Commands psql Gems Executing Shell Commands Lists and Structures Importing and Exporting Data Basic Reporting 31 32 33 34 35 35 36 36 36 37 37 38 39 Using pgAdmin 43 Getting Started Overview of Features Connecting to a PostgreSQL server Navigating pgAdmin pgAdmin Features Accessing psql from pgAdmin Editing postgresql.conf and pg_hba.conf from pgAdmin Creating Databases and Setting Permissions Backup and Restore pgScript Graphical Explain iv | Table of Contents www.it-ebooks.info 43 43 44 44 45 45 47 47 48 51 54 Job Scheduling with pgAgent Installing pgAgent Scheduling Jobs Helpful Queries 55 55 56 57 Data Types 59 Numeric Data Types Serial Generate Series Function Arrays Array Constructors Referencing Elements in An Array Array Slicing and Splicing Character Types String Functions Splitting Strings into Arrays, Tables, or Substrings Regular Expressions and Pattern Matching Temporal Data Types Time Zones: What It Is and What It Isn’t Operators and Functions for Date and Time Data Types XML Loading XML Data Querying XML Data Custom and Composite Data Types All Tables Are Custom Building Your Own Custom Type 59 59 60 60 60 61 61 62 63 63 64 65 66 68 70 70 70 71 71 71 Of Tables, Constraints, and Indexes 73 Tables Table Creation Multi-Row Insert An Elaborate Insert Constraints Foreign Key Constraints Unique Constraints Check Constraints Exclusion Constraints Indexes PostgreSQL Stock Indexes Operator Class Functional Indexes Partial Indexes Multicolumn Indexes 73 73 75 75 77 77 78 78 79 79 79 81 81 82 82 Table of Contents | v www.it-ebooks.info SQL: The PostgreSQL Way 85 SQL Views Window Functions Partition By Order By Common Table Expressions Standard CTE Writeable CTEs Recursive CTE Constructions Unique to PostgreSQL DISTINCT ON LIMIT and OFFSET Shorthand Casting ILIKE for Case Insensitive Search Set Returning Functions in SELECT Selective DELETE, UPDATE, and SELECT from Inherited Tables RETURNING Changed Records Composite Types in Queries 85 87 88 89 90 91 92 92 93 93 94 94 94 95 95 96 96 Writing Functions 99 Anatomy of PostgreSQL Functions Function Basics Trusted and Untrusted Languages Writing Functions with SQL Writing PL/pgSQL Functions Writing PL/Python Functions Basic Python Function Trigger Functions Aggregates 99 99 100 101 103 103 104 105 107 Query Performance Tuning 111 EXPLAIN and EXPLAIN ANALYZE Writing Better Queries Overusing Subqueries in SELECT Avoid SELECT * Make Good Use of CASE Guiding the Query Planner Strategy Settings How Useful Is Your Index? Table Stats Random Page Cost and Quality of Drives Caching vi | Table of Contents www.it-ebooks.info 111 113 114 116 116 118 118 118 120 120 121 10 Replication and External Data 123 Replication Overview Replication Lingo PostgreSQL Built-in Replication Advancements Third-Party Replication Options Setting Up Replication Configuring the Master Configuring the Slaves Initiate the Replication Process Foreign Data Wrappers (FDW) Querying Simple Flat File Data Sources Querying More Complex Data Sources 123 123 124 125 125 125 126 127 127 128 128 Appendix: Install, Hosting, and Command-Line Guides 131 Table of Contents | vii www.it-ebooks.info www.it-ebooks.info ...www.it-ebooks.info PostgreSQL: Up and Running Regina Obe and Leo Hsu Beijing • Cambridge • Farnham • Kưln • Sebastopol • Tokyo www.it-ebooks.info PostgreSQL: Up and Running by Regina Obe and Leo Hsu... details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc PostgreSQL: Up and Running, the image of the elephant shrew, and related... includes the title, author, publisher, and ISBN For example: ? ?PostgreSQL: Up and Running by Regina Obe and Leo Hsu (O’Reilly) Copyright 2012 Regina Obe and Leo Hsu, 978-1-449-32633-3.” If you

Ngày đăng: 11/07/2018, 14:33

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Preface

    • What Makes PostgreSQL Special and Why Use It?

    • Why Not PostgreSQL?

    • For More Information on PostgreSQL

    • Conventions Used in This Book

    • Using Code Examples

    • Safari® Books Online

    • How to Contact Us

    • Chapter 1. The Basics

      • Where to Get PostgreSQL

        • Notable PostgreSQL Forks

        • Administration Tools

        • What’s New in Latest Versions of PostgreSQL?

          • Why Upgrade?

          • What to Look for in PostgreSQL 9.2

          • PostgreSQL 9.1 Improvements

          • Database Drivers

          • Server and Database Objects

          • Where to Get Help

          • Chapter 2. Database Administration

            • Configuration Files

              • The postgresql.conf File

              • The pg_hba.conf File

                • Authentication Methods

                • Reload the Configuration Files

                • Setting Up Groups and Login Roles (Users)

                  • Creating an Account That Can Log In

Tài liệu cùng người dùng

Tài liệu liên quan