Step-by-Step PostgreSQL: Implement PostgreSQL self-starting

Source: Internet
Author: User

In the case of manual installation (for the case where the source code is compiled for PG or the decompressed version is installed for PG), PG does not start automatically when it is started, and stops automatically when it is turned off, as a DBA, this is obviously unacceptable.

1. Self-starting services in Windows

In Windows, you can use the pg_ctl command to generate the postgresql service and enable it to start itself. In fact, the installation version does the same. Let's take a look at the Detailed Help of the pg_ctl command first:

D:\pg921>pg_ctl --helppg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.Usage:  pg_ctl init[db]               [-D DATADIR] [-s] [-o "OPTIONS"]  pg_ctl start   [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"]  pg_ctl stop    [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]  pg_ctl restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]                 [-o "OPTIONS"]  pg_ctl reload  [-D DATADIR] [-s]  pg_ctl status  [-D DATADIR]  pg_ctl promote [-D DATADIR] [-s]  pg_ctl kill    SIGNALNAME PID  pg_ctl register   [-N SERVICENAME] [-U USERNAME] [-P PASSWORD] [-D DATADIR]                    [-S START-TYPE] [-w] [-t SECS] [-o "OPTIONS"]  pg_ctl unregister [-N SERVICENAME]Common options:  -D, --pgdata=DATADIR   location of the database storage area  -s, --silent           only print errors, no informational messages  -t, --timeout=SECS     seconds to wait when using -w option  -V, --version          output version information, then exit  -w                     wait until operation completes  -W                     do not wait until operation completes  -?, --help             show this help, then exit(The default is to wait for shutdown, but not for start or restart.)If the -D option is omitted, the environment variable PGDATA is used.Options for start or restart:  -c, --core-files       not applicable on this platform  -l, --log=FILENAME     write (or append) server log to FILENAME  -o OPTIONS             command line options to pass to postgres                         (PostgreSQL server executable) or initdb  -p PATH-TO-POSTGRES    normally not necessaryOptions for stop or restart:  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"Shutdown modes are:  smart       quit after all clients have disconnected  fast        quit directly, with proper shutdown  immediate   quit without complete shutdown; will lead to recovery on restartAllowed signal names for kill:  ABRT HUP INT QUIT TERM USR1 USR2Options for register and unregister:  -N SERVICENAME  service name with which to register PostgreSQL server  -P PASSWORD     password of account to register PostgreSQL server  -U USERNAME     user name of account to register PostgreSQL server  -S START-TYPE   service start type to register PostgreSQL serverStart types are:  auto       start service automatically during system startup (default)  demand     start service on demandReport bugs to <pgsql-bugs@postgresql.org>.

As shown in the preceding figure, pg_ctl register is used to generate a service, while pg_ctl unregister-n <service name> is used to delete a service.

For example:

D: \ pg921> pg_ctl register-N pg921-d: \ pg921 \ data-s Auto-w-T 10-l D: /pg921/log/pg921.log-o "-P 5433"

This command is used to generate a service: pg921. the startup mode is-s Auto. It is self-starting. If you want to generate a manual start, use-s demand.

-T 10, meaning waiting for 10 seconds. In fact, it can be set to be longer (in the production environment ).

-L D:/pg921/log/pg921.log, specify the location of the generated log file.

-O "-P 5433", change the service port number to 5433.

Verify the result of the above command generation:

D:\pg921>net start pg921The pg921 service is starting.The pg921 service was started successfully.D:\pg921>psql -p 5433 iiheropsql (9.2.1)Type "help" for help.iihero=# \q

2. self-starting services in Linux

In Linux, We need to write a self-starting script that supports at least two Command Options: Start and Stop, and create an appropriate link for this script. Taking ubuntu10 as an example,

First, check whether the system has the chkconfig command tool:

Xionghe @ seanlinux2 :~ $ Chkconfig
The program "chkconfig" has not been installed. You can use the following command to install:
Sudo apt-Get install chkconfig
Xionghe @ seanlinux2 :~ $ Sudo apt-Get install chkconfig

The script content is as follows: Put it under the/etc/init. d directory

#! /bin/sh# Installation prefixprefix=/home/xionghe/pgsql# Data directoryPGDATA="/home/xionghe/pgsql/data"# Who to run the postmaster as, usually "postgres".  (NOT "root")PGUSER=xionghe# Where to keep a log filePGLOG="$PGDATA/serverlog"# It's often a good idea to protect the postmaster from being killed by the# OOM killer (which will tend to preferentially kill the postmaster because# of the way it accounts for shared memory).  Setting the OOM_ADJ value to# -17 will disable OOM kill altogether.  If you enable this, you probably want# to compile PostgreSQL with "-DLINUX_OOM_ADJ=0", so that individual backends# can still be killed by the OOM killer.#OOM_ADJ=-17## STOP EDITING HERE# The path that is to be used for the scriptPATH=/home/xionghe/pgsql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin# What to use to start up the postmaster.  (If you want the script to wait# until the server has started, you could use "pg_ctl start -w" here.# But without -w, pg_ctl adds no value.)DAEMON="$prefix/bin/postmaster"# What to use to shut down the postmasterPGCTL="$prefix/bin/pg_ctl"set -e# Only start if we can find the postmaster.test -x $DAEMON ||{echo "$DAEMON not found"if [ "$1" = "stop" ]then exit 0else exit 5fi}# Parse command line parameters.case $1 in  start)echo -n "Starting PostgreSQL: "test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adjsu - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1echo "ok";;  stop)echo -n "Stopping PostgreSQL: "su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"echo "ok";;  restart)echo -n "Restarting PostgreSQL: "su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adjsu - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1echo "ok";;  reload)        echo -n "Reload PostgreSQL: "        su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"        echo "ok"        ;;  status)su - $PGUSER -c "$PGCTL status -D '$PGDATA'";;  *)# Print helpecho "Usage: $0 {start|stop|restart|reload|status}" 1>&2exit 1;;esacexit 0

Create a link:

Root @ seanlinux2:/etc # ln-S/etc/init. d/PostgreSQL/etc/rc0.d/k02postgresql
Root @ seanlinux2:/etc # ln-S/etc/init. d/PostgreSQL/etc/rc1.d/k02postgresql
Root @ seanlinux2:/etc # ln-S/etc/init. d/PostgreSQL/etc/rc2.d/k02postgresql
Root @ seanlinux2:/etc # ln-S/etc/init. d/PostgreSQL/etc/rc3.d/k98postgresql
Root @ seanlinux2:/etc # ln-S/etc/init. d/PostgreSQL/etc/rc4.d/k98postgresql
Root @ seanlinux2:/etc # ln-S/etc/init. d/PostgreSQL/etc/rc5.d/k98postgresql

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.