We imagine that you’re eager to test your newly installed server, so this concluding section guides you through a few steps that will help you to verify whether everything is running properly.
590 C H A P T E R 2 5 ■ I N S T A L L I N G P O S T G R E S Q L
The chapters that follow go into significant detail regarding some of the concepts discussed in this section, so this is intended only as a brief overview to get you started.
■ Note If you intend to operate PostgreSQL on any publicly accessible server, you are strongly urged to browse ahead to the opening sections of Chapter 29, which outline numerous steps that you should take to secure a PostgreSQL server.
On both Linux and Windows, you can determine whether the daemon is presently running by using the pg_ctl command. On Linux the command should be executed like this:
postgres$ pg_ctl status -D /usr/local/pgsql/data
On Windows, you need to change the path accordingly, but the command is the same:
%>pg_ctl status -D C:\pgsql\data
■ Tip You can forego specifying the data directory by setting the PGDATA environment variable, one of numerous variables that can affect your interaction with PostgreSQL. On Linux this is accomplished by modifying either /etc/profile, which makes the variable available to all users, or, for a specific user, the user’s shell- specific configuration file (for example, .bash_profile). Then, either log out and log back in, or execute the source command if your shell supports it. On Windows this is accomplished by navigating to the Windows Control Panel directory and choosing the System panel. Click Advanced, and then Environment Variables.
Click the New button, and assign PGDATA to the Variable name field, and assign the corresponding data path to the Variable value field. You’ll need to log on and back off for these changes to take effect.
If the daemon isn’t running, you’ll see the following message:
pg_ctl: neither postmaster nor postgres running
If the daemon is running, you’ll see something to the effect of:
pg_ctl: postmaster is running (PID: 4348)
/usr/local/pgsql/bin/postmaster "-D" "/usr/local/pgsql/data"
Because this is your first encounter with PostgreSQL, it’s presumed that the server is offline. To start it, execute the following command:
postgres$ pg_ctl start -D /usr/local/pgsql/data -l /usr/local/pgsql/logs/logfile &
This starts PostgreSQL, using the database cluster residing in /usr/local/pgsql/data, and logging messages to a file named logfile found in /usr/local/pgsql/logs. The logs directory doesn’t exist, by default, so you need to create it if you want to follow this example verbatim.
Of course, it’s recommended that you consider logging PostgreSQL information to a centralized logging location along with other services, although the process for doing so is beyond the scope of this book.
Again, if you’re running Windows, you need to change the paths accordingly. Of course, if you chose to install PostgreSQL as a service, then you can both start and stop it within the Services control panel.
Finally, you can verify PostgreSQL is running by logging into it using the psql client:
postgres$ psql template1
Should you receive a welcome message from psql, everything is working as intended! Note that psql is introduced in significant detail in Chapter 27.
Summary
This chapter set the stage for beginning your experimentation with the PostgreSQL server. You learned not only how to download and install PostgreSQL, but also how to configure it to best fit your administrative and application needs. Other configuration issues are revisited throughout the remainder of this book as necessary.
In the next chapter you’ll learn how to effectively manage your newly installed PostgreSQL server, beginning with a detailed introduction to pg_ctl. You’ll also learn more about advanced configuration options, how to create and manage tablespaces, how to perform system main- tenance tasks using Vacuum and Analyze, and how to both back up and recover your data.
593
■ ■ ■
C H A P T E R 2 6
PostgreSQL Administration
There is an old saying that “with great power, comes great responsibility.” Though not origi- nally uttered in reference to database software, it certainly seems a fitting way to describe the current systems that exist in the market. If you want a powerful, “enterprise class” system, you likely have to deal with a system that requires an onerous amount of tuning with an exorbitant number of knobs and switches, many of which have nothing to do with the feature you really intend to use. On the other end are database systems that may be quick and easy to install, but provide only the most basic database functionality. With PostgreSQL, the developers have tried to strike a balance between a high level of features and tuning parameters, but implemented in a way that is almost hidden from those who do not need to use those options. This makes PostgreSQL one of the easiest databases to administer, even when working within an enterprise- critical environment, without having to sacrifice functionality.
In this chapter, we take a look at some of these knobs and switches that you can use to help tune your PostgreSQL system for maximum performance, and go over some of the basic system maintenance tasks that you should be aware of if you are going to administer a PostgreSQL system. By the end of this chapter you should be familiar with:
• Starting and stopping a PostgreSQL server
• The configuration variables most important for system tuning and smooth operation
• Creating and working with tablespaces
• System maintenance tasks, including VACUUM and ANALYZE
• How to upgrade between versions of PostgreSQL
• Backup and recovery of a PostgreSQL system
While working with these options is not hard, it is an important topic. Even if you don’t need to maintain a production environment, many of the tasks described here will be helpful for keeping your own development systems up and running.