< Day Day Up > |
18.3. PostgreSQLTo get the source distribution of PostgreSQL, download the latest tarball from http://www.postgresql.org/download. At the time of this writing, the latest release was 8.0.1, so we downloaded postgresql-8.0.1.tar.bz2. 18.3.1. Compiling PostgreSQLBefore installing PostgreSQL, you must install readline (http://www.gnu.org/directory/readline.html). This program enables support for command-line editing and history in the PostgreSQL shell (psql). Use fink install readline to install it, if you have Fink installed. To compile PostgreSQL from source:
18.3.2. Installing PostgreSQLIf everything went OK, you're ready to install. If it didn't go OK, check the PostgreSQL mail list archives (http://www.postgresql.org/lists.html) to see if anyone has reported the same problem you experienced and whether a fix is available (otherwise, you should submit a bug report).
18.3.3. Adding the Startup ItemNow you're ready to create a startup script for PostgreSQL (see "Startup Items" in Chapter 4). First, create the script shown in Example 18-1, save it as /Library/StartupItems/PostgreSQL/PostgreSQL, and mark it as an executable. Example 18-1. Startup script for PostgreSQL#!/bin/sh # Source common setup, including hostconfig. # . /etc/rc.common StartService( ) { # Don't start unless PostgreSQL is enabled in /etc/hostconfig if [ "${PGSQL:=-NO-}" = "-YES-" ]; then ConsoleMessage "Starting PostgreSQL" sudo -u postgres /usr/local/pgsql/bin/pg_ctl \ -D /usr/local/pgsql/data \ -l /usr/local/pgsql/data/logfile start fi } StopService( ) { ConsoleMessage "Stopping PostgreSQL" su postgres -c \ "/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop" } RestartService( ) { # Don't restart unless PostgreSQL is enabled in /etc/hostconfig if [ "${PGSQL:=-NO-}" = "-YES-" ]; then ConsoleMessage "Restarting PostgreSQL" StopService StartService else StopService fi } RunService "$1" Next, create the following file as /Library/StartupItems/PostgreSQL/StartupParameters.plist: { Description = "PostgreSQL"; Provides = ("PostgreSQL"); Requires = ("Network"); OrderPreference = "Late"; } Then, add the following line to /etc/hostconfig: PGSQL=-YES- Now PostgreSQL will start automatically when you reboot the system. If you want, you can start PostgreSQL right away with: $ sudo SystemStarter start PostgreSQL 18.3.4. Configuring PostgreSQLBefore you proceed, you should add the following line to the .bash_profile and start a new Terminal window to pick up the settings (you should also add this to the postgres user's .bash_profile): export PATH=$PATH:/usr/local/pgsql/bin By default, PostgreSQL comes with weak permissions; any local user can connect to the database without authentication. Before changing anything, you must start a shell as the postgres user with sudo and stay in this shell until the end of this section: $ sudo -u postgres -s Password: ******** postgres$ To start locking things down and to set up a non-privileged user:
To give more than one user access to a database, create a group with the same name as the database (for example, create group databasename), and create users with the create user command as shown in step 5. Finally, add each user to the group with this command: alter group databasename add user username 18.3.5. Using PostgreSQLAfter configuring PostgreSQL's security and setting up an unprivileged user, you can log in as that user and play around with the database: $ psql -U username Password: ******** Welcome to psql 7.4, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit username=> CREATE TABLE foo (bar CHAR(10)); CREATE TABLE username=> INSERT INTO foo VALUES('Hello'); INSERT 17148 1 username=> INSERT INTO foo VALUES('World'); INSERT 17149 1 username=> SELECT * FROM foo; bar ------------ Hello World (2 rows) username-> \q For more information on building and using PostgreSQL, see Practical PostgreSQL by John C. Worsley and Joshua D. Drake (O'Reilly). Practical PostgreSQL covers installing, using, administrating, and programming PostgreSQL. |
< Day Day Up > |