2012-12-10 95 views
0

我有一個腳本,當我的debian 6.0服務器部署時運行,它的目的是從源碼創建postgreSQL,爲它創建一個系統用戶,創建一個數據庫並啓動它運行。我是新來這一點,但我做了很多功課,這就是我想出了:Bash腳本安裝PostgreSQL - 不工作

# 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 

由於這是在部署運行其難以調試了什麼錯誤,但我可以看到下載並安裝postgres似乎工作正常。但是,我知道的是postgres沒有運行在我的服務器上。我想知道我的代碼的最後一部分,如果有人可以看到任何我不正確的行爲,這可能會導致這種情況?

+0

只需從shell運行腳本並查看它是否吐出錯誤。 – PeeHaa

+1

PeeHaa的建議是最好的主意。只是爲了提供一個替代方案,如果您的postgres正在安裝但不能啓動,請查看數據目錄中的日誌文件,因爲它們可能包含與啓動相關的錯誤。 –

+0

運行sudo /etc/init.d/postgresql開始和發佈錯誤 – Baconator507

回答

4

腳本中明顯錯誤的部分是,它預計su - postgres之後的行將作爲postgres用戶運行。這不會發生。

在批處理模式下,su - postgres開始並立即退出,因爲沒有命令送入它。然後,腳本的下一個命令將在用戶啓動腳本時執行(假定爲root),並且失敗。

相反,你應該寫這樣的事情:

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 

的意見建議,假設你已經通過軟件包安裝PostgreSQL的,但是這不是問題的情況下。當你從源代碼安裝./configure沒有參數和make install,它將永遠不會在/usr/local/pgsql之外安裝任何東西。在這種情況下,在/etc下沒有啓動腳本是很正常的。