#1
  1. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    14

    Help - Starting Postgres on Startup with at different default DB location


    TL:DR - I installed Postgres, but need to make sure it starts up when the server starts. But I can't seem to find the startup script and how it passes in the commands to tell it where the postgres config and database location is. I also need to move the DB from the default location.

    Will try and be as detailed as I can. I am not a linux person, so I'll be honest, I don't know much.

    I have a freshly installed Ubuntu 14.04 LTS Server. I installed Postgres 9.3.10. The server runs on an SSD, with HDD as data drives. But I need to move the DB to be on the HDD

    I installed via apt-get PostgreSQL 9.3.10:

    Code:
    sudo apt-get install postgresql postgresql-contrib
    It installed fine.

    I ran the following to see to verify it was running:

    Code:
    ps auxw |  grep postgres | grep -- -D
    and the results were:

    Code:
    postgres  1032  0.0  0.1 247760 21048 ?        S    Oct19   0:10 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf
    So, it installed to: /usr/lib/postgresql/9.3
    Database is located: /var/lib/postgresql/9.3/main
    And the config file is: /etc/postgresql/9.3/main/postgres.conf

    My end goals are to 1. Make sure that Postgres gets started if the computer reboots/on startup. 2. Move the DB from its current location on the SSD, to another location on the HDD (e.g. /mnt/drive/pg/db)

    I figure the steps needed are:

    Code:
    Stop postgres
    Move the Database Files (sudo mv /path/to/location  /new/path/to/new/location)
    Change the startup script to account for new location
    Add to init.d while passing in arguments to give it new location etc?
    restart postgres
    If I open /etc/postgresql/9.3/main/postgresql.conf, I can see the following settings:

    Code:
    # The default values of these variables are driven from the -D command-line
    # option or PGDATA environment variable, represented here as ConfigDir.
    
    
    data_directory = '/var/lib/postgresql/9.3/main'         # use data in another directory
                                            # (change requires restart)
    hba_file = '/etc/postgresql/9.3/main/pg_hba.conf'       # host-based authentication file
                                            # (change requires restart)
    But I decide to look for where postgres is started up, as it looks to be starting up with custom arguments/commands (grab postgres.conf from /etc/..., postgres from /usr/lib/postgresql/9.3/bin , and the actual DB file from /var/lib/postgresql/9.3/main . (Based on PostgreSQL: Documentation: 9.3: Starting the Database Server where you pass in the -D argument and tell it all of that)

    I checked /etc/init/ and I don't see anything related to postgres

    I check /etc/init.d/ and I see postgres.

    I check the postgres

    Code:
    vi postgres
    And It looks like it just takes in a version, and start/stop/restart/reload. I don't see anything in regards to starting up postgres as shown above (where it seems to have been passed in a custom location for the DB and config file).

    Code:
    #!/bin/sh
    set -e
    
    ### BEGIN INIT INFO
    # Provides:             postgresql
    # Required-Start:       $local_fs $remote_fs $network $time
    # Required-Stop:        $local_fs $remote_fs $network $time
    # Should-Start:         $syslog
    # Should-Stop:          $syslog
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    PostgreSQL RDBMS server
    ### END INIT INFO
    
    # Setting environment variables for the postmaster here does not work; please
    # set them in /etc/postgresql/<version>/<cluster>/environment instead.
    
    [ -r /usr/share/postgresql-common/init.d-functions ] || exit 0
    
    . /usr/share/postgresql-common/init.d-functions
    
    # versions can be specified explicitly
    if [ -n "$2" ]; then
        versions="$2 $3 $4 $5 $6 $7 $8 $9"
    else
        get_versions
    fi
    
    case "$1" in
        start|stop|restart|reload)
            if [ -z "`pg_lsclusters -h`" ]; then
                log_warning_msg 'No PostgreSQL clusters exist; see "man pg_createcluster"'
                exit 0
            fi
            for v in $versions; do
                $1 $v || EXIT=$?
            done
            exit ${EXIT:-0}
            ;;
        status)
            LS=`pg_lsclusters -h`
            # no clusters -> unknown status
            [ -n "$LS" ] || exit 4
            echo "$LS" | awk 'BEGIN {rc=0} {if (match($4, "down")) rc=3; printf ("%s/%s (port %s): %s\n", $1, $2, $3, $4)}; END {exit rc}'
            ;;
        force-reload)
            for v in $versions; do
                reload $v
            done
            ;;
        *)
            echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [version ..]"
            exit 1
            ;;
    esac
    
    exit 0
    I then try this command:

    Code:
    initctl list
    but i do not see postgres in that list either

    So then I do the following command:

    Code:
    sudo service --status-all
    And I see this:

    Code:
    [ + ]  postgresql
    Not sure what that means.

    I did check other software in the list, that I know should startup, but those have a ? in the box (there are + - ? in the boxes)

    So that is where I am now. I am lost as to how postgres gets automatically started on boot (if it does) and how it passes in the arguments to tell it where the config/DB file is (since they are at different locations).

    Any help would greatly be appreciated
    Last edited by jaeSun; October 21st, 2015 at 12:01 PM.
  2. #2
  3. No Profile Picture
    got Rice?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Posts
    532
    Rep Power
    14
    apparently, I was 99% of the way there.

    I didn't see the include line in the /etc.d/postgresql startup script, where it starts up postgresql with the options from postgresql.conf. So I just needed to change the options in postgresql.conf and restart and all is good.

IMN logo majestic logo threadwatch logo seochat tools logo