(学习Bash)如何从Bash文件运行多个postgresql命令
这就是我正在手动执行的操作(学习Bash)如何从Bash文件运行多个postgresql命令,bash,postgresql,ubuntu,Bash,Postgresql,Ubuntu,这就是我正在手动执行的操作 user@host: sudo -u postgres psql postgres=# create database myproj; postgres=# create user myuser with password 'mypass'; postgres=# alter role myuser set client_encoding to 'utf8'; postgres=# alter role myuser set default_transaction_i
user@host: sudo -u postgres psql
postgres=# create database myproj;
postgres=# create user myuser with password 'mypass';
postgres=# alter role myuser set client_encoding to 'utf8';
postgres=# alter role myuser set default_transaction_isolation to 'read committed';
postgres=# alter role myuser set timezone to 'UTC';
postgres=# grant all privileges on database myproj to myuser;
postgres=# \q
问题是,如果要将其保存到.sh文件并运行,内容应该是什么。像这样的
sudo -u postgres psql -c "create database myproj;"
sudo -u postgres psql -c "create user myuser with password 'mypass';"
sudo -u postgres psql -c "alter role myuser set client_encoding to 'utf8';"
sudo -u postgres psql -c "alter role myuser set default_transaction_isolation to 'read committed';"
sudo -u postgres psql -c "alter role myuser set timezone to 'UTC';"
sudo -u postgres psql -c "grant all privileges on database myproj to myuser;"
您可以使用此处的文档
以同样的方式运行它
sudo -u postgres psql -U <yourusername> -d <yourdbname> -f dbscript.sql
sudo-u postgres psql-u-d-f dbscript.sql
或
sudo-u postgres psql我无法保存sql文件,所以我尝试第一种方法。我已经在pastebin准备了.sh文件。如果可能,请检查我是否正确理解了您的答案?第一个和最后一个cd命令只是为了理解这是大.sh文件的一部分。可以吗?@Rahul:cdcd
命令不需要。休息似乎没问题。请在运行之前在您的测试环境中进行验证。cd
命令用于确认这是较大的.sh文件的一部分,并且可以以这种方式工作。
$cat dbscript.sql
create database myproj;
create user myuser with password 'mypass';
..
sudo -u postgres psql -U <yourusername> -d <yourdbname> -f dbscript.sql
sudo -u postgres psql <<INP
\i /path/dbscript.sql