ubuntu psql命令未在bash文件中执行[重复]

laximzn5  于 2023-01-04  发布在  其他
关注(0)|答案(2)|浏览(146)
    • 此问题在此处已有答案**:

Pass commands as input to another command (su, ssh, sh, etc)(3个答案)
22小时前关门了。
我有以下代码在我的bash脚本安装golang和psql服务器,我想创建一个用户名为"admin"和密码为"admin"的用户。运行脚本后,它将进入psql服务器,但psql -c "CREATE USER admin WITH PASSWORD 'admin';"没有得到执行。

#!/bin/bash

# Install golang
echo "Installing required package"
sudo apt-get update
sudo apt install snapd
snap install go --classic

# Install postgreSQL
echo "Installing postgreSQL"
sudo apt-get update
sudo apt install postgresql postgresql-contrib postgresql-client

# Starting database
echo "Starting Database"
sudo service postgresql start
sudo -u postgres psql

# Creating user "admin" with password "admin"
echo "Creating user 'admin' with password 'admin'"
psql -c "CREATE USER admin WITH PASSWORD 'admin';"
wfveoks0

wfveoks01#

#!/bin/bash

# Install golang
echo "Installing required package"
sudo apt-get update
sudo apt install snapd
snap install go --classic

# Install postgreSQL
echo "Installing postgreSQL"
sudo apt-get update
sudo apt install postgresql postgresql-contrib postgresql-client

# Starting database
echo "Starting Database"
sudo service postgresql start

# Creating user "admin" with password "admin"
sudo -u postgres psql -c "CREATE USER admin WITH PASSWORD 'admin';"

我通过删除sudo -u postgres psql并使用sudo -u postgres psql -c "CREATE USER admin WITH PASSWORD 'admin';"修复了该问题,现在运行良好

z9ju0rcb

z9ju0rcb2#

echo "正在启动数据库" sudo服务postgresql启动

完成此步骤后。(只需切换到postgres)

苏波斯特格雷
psql-c "使用密码" admin "创建用户管理员; "

相关问题