Bash Script to install PostgreSQL - Not working

2019-06-08 08:32发布

I have a script which runs when my debian 6.0 server is deployed and it is designed to build postgreSQL from source, create a system user for it, create a database and start it running. I am new to this but I did a lot of homework and this is what I came up with:

# Initial
    apt-get update
    apt-get -y install aptitude bzip2 libbz2-dev git-core bison flex
    aptitude -y install sudo python-all-dev python-setuptools libxml2-dev libgeoip-dev libxslt1-dev uuid-dev gcc automake autoconf libpcre3-dev libssl-dev unzip zip python-psycopg2 libpq-dev wget make libreadline-dev
    aptitude -y full-upgrade 


# POSTGRESQL
###############################

# Postgresql Download & Install
    wget http://ftp.postgresql.org/pub/source/v8.4.6/postgresql-8.4.6.tar.gz -P /tmp
    mkdir /tmp/postgresql
    tar xzf /tmp/postgresql-8.4.6.tar.gz -C "/tmp/postgresql"
    cd /tmp/postgresql/postgresql-8.4.6
    ./configure
    make
    make install

# Add User
    useradd postgres
    chown "postgres" /usr/local/pgsql
    mkdir /usr/local/pgsql/data
    chown postgres:postgres /usr/local/pgsql/data
    su - postgres
    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
    /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data
    /usr/local/pgsql/bin/createdb test

Since this is run on deployment its difficult to debug what is going wrong, but I can see that the download and install of postgres seems to work ok. However, what I do know is postgres isnt running on my server. I was wondering for the last part of my code if anyone can see anything I am doing incorrectly which might cause this?

1条回答
男人必须洒脱
2楼-- · 2019-06-08 08:57

The part that is clearly wrong in your script is that it expects the lines following the su - postgres to be run as the postgres user. This won't happen.

In batch mode, su - postgres starts and immediately exits because no command is fed to it. Then the next commands of the scripts are executed as the user launching the script (presumably root) and they fail.

Instead, you should write something like this:

su - postgres <<-'EOF'
  /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
  /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data
  /usr/local/pgsql/bin/createdb test
EOF
# the lines after the EOF will be executed again as the initial user

The suggestions in the comments assume that you have installed postgresql via a package, but that's not the context of the question. When you install from source with ./configure without arguments and make install, it will never install anything outside /usr/local/pgsql. It's perfectly normal to have no startup script under /etc in this context.

查看更多
登录 后发表回答