能够在命令行中创建postgres数据库,但不能在bash脚本中创建 [英] Able to create postgres database in command line but not in bash script

查看:74
本文介绍了能够在命令行中创建postgres数据库,但不能在bash脚本中创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个bash脚本来自动创建和填充数据库.

I am trying to write a bash script that automates created and filling a database.

我发现我可以使用以下命令在命令行中创建数据库:

I found that I can use these commands to create a database in the command line:

sudo su postgres
psql -c 'CREATE DATABASE routing;'
exit

之后我会看到数据库.但是,当我将它们放入shell脚本并运行(使用sudo)时,不会创建数据库.有什么想法吗?

I see the database afterwards. However when I put these into a shell script and run it (with sudo), the database is not created. Any ideas?

推荐答案

sudo su 之后,您将成为另一个用户.此时,脚本将停止执行,直到退出用户为止,然后它将继续执行.

after you sudo su you become another user. at this point script will stop executing, until you exit the user, then it will go on executing.

相反,如果您想将某些东西作为postgres运行,请尝试如下所示:

instead if you want to run something as postgres , try smth like this:

sudo su postgres <<EOF
psql -c 'CREATE DATABASE routing;'
EOF
exit

这篇关于能够在命令行中创建postgres数据库,但不能在bash脚本中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆