: Richard Stones, Neil Matthew
: Beginning Databases with PostgreSQL From Novice to Professional
: Apress
: 9781430200185
: 2
: CHF 83.90
:
: Informatik
: English
: 664
: Wasserzeichen/DRM
: PC/MAC/eReader/Tablet
: PDF
*The most updated PostgreSQL book on the market, covering version 8.0

*Highlights the most popular PostgreSQL APIs, including C, Perl, PHP, and Java

*This is two books in one; it simultaneously covers key relational database design principles, while teaching PostgreSQL



Richard Stones graduated from university with an electrical engineering degree, but decided software was more fun. He has programmed in a variety of languages, but only admits to knowing Visual Basic under duress. He has worked for a number of companies, from the very small to the very large, in a variety of areas, from real-time embedded systems upward. He is employed by Celesio AG as a systems architect, working principally on systems for the retail side of the business. He has co-authored several computing books with Neil Matthew, including Beginning Linux Programming, Professional Linux Programming, and Beginning Databases with MySQL.

Accessing PostgreSQL from Perl (S. 465-466)

As earlier chapters have shown, communicating with PostgreSQL generally involves a lot of string manipulation. One language that excels at string manipulation is Perl. In Chapter 13, we demonstrated that the libpq interface is a powerful way to access a PostgreSQL database, but there are disadvantages. We need to use string manipulation to pass values to queries and to retrieve results, and for short programs, the C code dealing with strings can overshadow the database interactions.

As Chapter 13 pointed out, although binary access is possible, its benefits are minimal. With Perl, strings are much more sophisticated, supporting functionality such as joining, splitting, pattern matching, and automatic conversion to and from other data types. Perl has also historically been associated with web server processing (although more modern mechanisms such as PHP, described in the previous chapter, are taking over that role).

Having interfaces to databases definitely adds benefits. If you know even a little about Perl, you will be aware that one of the language’s axioms is that there is always more than one way to tackle any given job. In fact, Perl enthusiasts would be disappointed if they had to limit their options to single figures. We do not propose to bombard you with numerous techniques for accessing PostgreSQL databases from Perl, however. Instead, we will present a single methodology. There are essentially three ways to access PostgreSQL from Perl:

• Low-level access, which is essentially a Perl mapping of the libpq C interface (Module Pg)
• High-level access, using a database independent layer (DBI)
• Access by embedding the Perl interpreter (similar to the description in Chapter 14)

We will describe only the high-level DBI access mechanism, because it is the simplest to install and use. This method is database-independent, yet is still very flexible and powerful. If you are interested in a libpq-style of working, we suggest taking a look at Module Pg, which is part of the DBD:: Pg database driver. PL/Perl requires a version of Perl to have been initially built as a shared library - libperl.so, as opposed to the more usual libperl.a (see the instructions for building Perl found in Perl source distributions).

Installing Perl Modules

The Perl programming language supports the concept of modules - additional functions that can be integrated into a Perl installation to provide extra features. Many developers have developed modules to extend Perl, providing diverse functionality, including network protocols for file transfer or sending e-mail, parsing and manipulating XML documents, generating graphical images, and more. Much of Perl’s standard functionality is provided by modules included with a basic Perl installation.

In this chapter, we will be using several modules that are not included in the base Perl installation. Because installing modules is a fairly common task, the Perl community has developed a standard process for finding and installing modules. This process has itself been encapsulated in a Perl module, the CPAN module, which we will meet in a moment. Perl modules can be written in several languages, not just Perl. Modules written in a language other than Perl need to be compiled before they can be installed, but binaries are often available to download.

Contents6
About the Authors18
About the Technical Reviewer20
Acknowledgments21
Introduction22
Chapter 1 Introduction to PostgreSQL24
Programming with Data24
Constant Data25
Flat Files for Data Storage25
Repeating Groups and Other Problems26
What Is a Database Management System?27
Database Models27
Query Languages31
Database Management System Responsibilities33
What Is PostgreSQL?34
Short History of PostgreSQL35
The PostgreSQL Architecture36
Data Access with PostgreSQL38
What Is Open Source?38
Resources39
Chapter 2 Relational Database Principles40
Limitations of Spreadsheets40
Storing Data in a Database44
Choosing Columns44
Choosing a Data Type for Each Column44
Identifying Rows Uniquely45
Accessing Data in a Database46
Accessing Data Across a Network47
Handling Multiuser Access48
Slicing and Dicing Data49
Adding Information51
Using Multiple Tables51
Relating a Table with a Join Operation52
Designing Tables55
Understanding Some Basic Rules of Thumb56
Creating a Simple Database Design57
Extending Beyond Two Tables58
Completing the Initial Design60
Basic Data Types63
Dealing with the Unknown: NULLs64
Reviewing the Sample Database65
Summary65
Chapter 3 Getting Started with PostgreSQL66
Installing PostgreSQL on Linux and UNIX Systems66
Installing PostgreSQL from Linux Binaries67
Anatomy of a PostgreSQL Installation70
Installing PostgreSQL from the Source Code72
Setting Up PostgreSQL on Linux and UNIX76
Installing PostgreSQL on Windows82
Using the Windows Installer82
Configuring Client Access87
Creating the Sample Database87
Creating User Records88
Creating the Database88
Creating the Tables90
Removing the Tables91
Populating the Tables92
Summary95
Chapter 4 Accessing Your Data96
Using psql97
Starting Up on Linux Systems97
Starting Up on Windows Systems97
Resolving Startup Problems98
Using Some Basic psql Commands101
Using Simple SELECT Statements101
Overriding Column Names104
Controlling the Order of Rows104
Suppressing Duplicates106
Performing Calculations109
Choosing the Rows110
Using More Complex Conditions112
Pattern Matching114
Limiting the Results115
Checking for NULL116
Checking Dates and Times117
Setting the Time and Date Style117
Using Date and Time Functions121
Working with Multiple Tables123
Relating Two Tables123
Aliasing Table Names128
Relating Three or More Tables129
The SQL92 SELECT Syntax133
Summary135
Chapter 5 PostgreSQL Command-Line and Graphical Tools136
psql136
Starting psql137
Issuing Commands in psql137
Working with the Command History138
Scripting psql138
Examining the Database140
psql Command-Line Quick Reference141
psql Internal Commands Quick Reference142
ODBC Setup144
Installing the ODBC Driver144
Creating a Data Source146
pgAdmin III148
Installing pgAdmin III148
Using pgAdmin III149
phpPgAdmin152
Installing phpPgAdmin153
Using phpPgAdmin153
Rekall156
Connecting to a Database157
Creating Forms158
Building Queries159
Microsoft Access160
Using Linked Tables160
Entering Data and Creating Reports164
Microsoft Excel165
Resources for PostgreS