sql - 用于安装 PostgreSQL 的 Bash 脚本 - 失败

标签 sql bash postgresql shell debian

我构建了这个部署脚本,它在部署我的 debian 6.0 服务器时运行。我之前在这里展示过它(这是一个 linode stackscript,以防其他人想知道):

#!/bin/bash
#
# Install PostgreSQL
#
# Copyright (c) 2010 Filip Wasilewski <en@ig.ma>.
#
# My ref: http://www.linode.com/?r=aadfce9845055011e00f0c6c9a5c01158c452deb

function postgresql_install {
    aptitude -y install postgresql postgresql-contrib postgresql-dev libpq-dev
}

function postgresql_create_user {
    # postgresql_create_user(username, password)
    if [ -z "$1" ]; then
        echo "postgresql_create_user() requires username as the first argument"
        return 1;
    fi
    if [ -z "$2" ]; then
        echo "postgresql_create_user() requires a password as the second argument"
        return 1;
    fi

    echo "CREATE ROLE $1 WITH LOGIN ENCRYPTED PASSWORD '$2';" | sudo -i -u postgres psql
}

function postgresql_create_database {
    # postgresql_create_database(dbname, owner)
    if [ -z "$1" ]; then
        echo "postgresql_create_database() requires database name as the first argument"
        return 1;
    fi
    if [ -z "$2" ]; then
        echo "postgresql_create_database() requires an owner username as the second argument"
        return 1;
    fi

    sudo -i -u postgres createdb --owner=$2 $1
}

postgresql_install
postgresql_create_user(username, password)
postgresql_create_database(dbname, username)

我用这个脚本部署了我的服务器,它建立在 Filip 的版本之上,但是当我尝试通过键入 pg_ctl 查看 postgresql 是否正在运行时,它说找不到命令。

我哪里做错了?由于它是在服务器运行时部署的,所以我看不出哪里出错了。

最佳答案

正如人们所说,您的路径中似乎没有 PostgreSQL bin 目录。根据我在 Ubuntu 上使用 PostgreSQL 9.1/9.2 从 apt 安装的经验,创建了 postgres 用户,但它没有正确设置你的环境,所以 pg_ctlinitdb 等不在您的 PATH 上。

我看不到您使用的 PostgreSQL 版本,但我的 9.1 和 9.2 实例将二进制文件存储在 /usr/lib/postgresql/9.2/bin

检查该目录以查看它是否包含 pg_ctl 和其他二进制文件。如果是的话,

尝试运行:

export PATH=$PATH:/usr/lib/postgresql/9.2/bin

看看是否允许您运行 pg_ctl。如果是这样,您需要在登录时从 .bashrc 或类似的

执行它

关于sql - 用于安装 PostgreSQL 的 Bash 脚本 - 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13733472/

相关文章:

c# - 将逗号分隔的参数传递给 SQL 中的存储过程

linux - Bash 中 ${} 和 $() 的区别

Mysql 到 PostgreSql Rails 3 heroku 应用程序 true/false

mysql - MySQL if else endif 语句怎么写?

java - 如何使用 JOOQ 从模板和参数占位符生成 sql?

SQL-Azure 性能,添加数据库还是添加服务器?

bash - : and # for Bash comments 之间的差异

cocoa 或 Bash : Test if a file is an executable binary

sql - 返回数组的 unnest() 子查询?

ruby-on-rails - 使用 Postgresql 数据库创建 Rails 3.1 应用程序 (OSX Lion)