Linux 脚本自动postgres设置
我正在编写一个系统设置脚本,并且已经安装了postgres。下面是一个测试脚本(以root身份运行),用于尝试报告postgres中的工作目录。作为postgres调用Linux 脚本自动postgres设置,linux,postgresql,Linux,Postgresql,我正在编写一个系统设置脚本,并且已经安装了postgres。下面是一个测试脚本(以root身份运行),用于尝试报告postgres中的工作目录。作为postgres调用pwd将给出/var/lib/postgresql。但是测试 #!/bin/bash su - postgres pwd > /home/me/postgres_report exit 。。失败(显然)并报告原始工作目录。之后,bashshell卡在postgres中,这表明命令的调用顺序不正确。我理解这里的bash环境问
pwd
将给出/var/lib/postgresql
。但是测试
#!/bin/bash
su - postgres
pwd > /home/me/postgres_report
exit
。。失败(显然)并报告原始工作目录。之后,bashshell卡在postgres中,这表明命令的调用顺序不正确。我理解这里的bash环境问题。我不知道如何做我需要做的事情,那就是自动化一个postgres过程,我可以很容易地以交互方式完成(即,进入postgres,执行命令,然后退出)。有指针吗?使用sudo
使用以下其中一种:
- 将单行命令传递给
psql
sudo -u postgres psql -c "SELECT ..."`
- 这里有一份文件:
sudo -u postgres psql <<"__END__" SELECT ...; SELECT ...; __END__
postgres
系统用户,才能通过peer
身份验证以postgres
数据库用户身份运行任务时,才需要sudo-u postgres
。否则,您可以使用psql-U用户名
、一个.pgpass
文件等。#/bin/bash
#!/bin/bash
# run as root
[ "$USER" = "root" ] || exec sudo "$0" "$@"
echo "=== $BASH_SOURCE on $(hostname -f) at $(date)" >&2
sudo passwd postgres
echo start the postgres
sudo /etc/init.d/postgresql start
sudo su - postgres -c \
"psql <<__END__
SELECT 'crate the same user' ;
CREATE USER $USER ;
ALTER USER $USER CREATEDB;
SELECT 'grant him the priviledges' ;
grant all privileges on database postgres to $USER ;
alter user postgres password 'secret';
SELECT 'AND VERIFY' ;
select * from information_schema.role_table_grants
where grantee='""$USER""' ;
SELECT 'INSTALL EXTENSIONS' ;
CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";
CREATE EXTENSION IF NOT EXISTS \"pgcrypto\";
CREATE EXTENSION IF NOT EXISTS \"dblink\";
__END__
"
sudo /etc/init.d/postgresql status
sudo netstat -tulntp | grep -i postgres
#以根用户身份运行
[“$USER”=“root”]| | exec sudo“$0”“$@”
echo“==$(主机名-f)在$(日期)上的$BASH\u源文件”>&2
sudo passwd postgres
回音启动postgres
sudo/etc/init.d/postgresql start
sudo su-postgres-c\
“psql为什么不使用fabric,它非常适合这种情况?我应该提到我将脚本称为root,但我会检查一下
#!/bin/bash
# run as root
[ "$USER" = "root" ] || exec sudo "$0" "$@"
echo "=== $BASH_SOURCE on $(hostname -f) at $(date)" >&2
sudo passwd postgres
echo start the postgres
sudo /etc/init.d/postgresql start
sudo su - postgres -c \
"psql <<__END__
SELECT 'crate the same user' ;
CREATE USER $USER ;
ALTER USER $USER CREATEDB;
SELECT 'grant him the priviledges' ;
grant all privileges on database postgres to $USER ;
alter user postgres password 'secret';
SELECT 'AND VERIFY' ;
select * from information_schema.role_table_grants
where grantee='""$USER""' ;
SELECT 'INSTALL EXTENSIONS' ;
CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";
CREATE EXTENSION IF NOT EXISTS \"pgcrypto\";
CREATE EXTENSION IF NOT EXISTS \"dblink\";
__END__
"
sudo /etc/init.d/postgresql status
sudo netstat -tulntp | grep -i postgres