Linux 用于安装PostgreSQL的Bash脚本-不工作

Linux 用于安装PostgreSQL的Bash脚本-不工作,linux,bash,postgresql,shell,debian,Linux,Bash,Postgresql,Shell,Debian,我有一个脚本,它在部署debian 6.0服务器时运行,设计用于从源代码构建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-setupto

我有一个脚本,它在部署debian 6.0服务器时运行,设计用于从源代码构建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并没有在我的服务器上运行。我想知道我的代码的最后一部分是否有人能看到我做的任何不正确的事情,这可能会导致这种情况?

脚本中明显错误的部分是,它希望
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

su-postgres脚本中明显错误的部分是它希望
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

su-postgres只需从shell运行脚本,看看它是否会抛出错误。PeeHaa的建议是最好的主意。如果您的postgres正在安装但未启动,请查看data目录中的日志文件,因为它们可能包含与启动相关的错误。运行sudo/etc/init.d/postgresql start和post errors谢谢您的所有回复@Baconator507我得到这个:sudo:/etc/init.d/postgresql:command not foundtry service postgresql start只需从shell运行脚本,看看它是否会抛出错误。PeeHaa的建议是最好的主意。如果您的postgres正在安装但未启动,请查看data目录中的日志文件,因为它们可能包含与启动相关的错误。运行sudo/etc/init.d/postgresql start和post errors谢谢您的所有回复@Baconator507我得到这个:sudo:/etc/init.d/postgresql:command not foundtry service postgresql start