2014-11-25 77 views
5

我在PHP脚本中创建bash命令。内置命令如:psql - 角色根目录不存在

su postgres -c "for tbl in `psql -qAt -c \"select tablename from pg_tables where schemaname = 'public';\" demodoo` ;do psql -c \"alter table $tbl owner to postgres\" demodoo ;done " 

当我尝试在shell中运行这个命令,我得到这个错误:

psql: FATAL: role "root" does not exist 

这是为什么发生的历史,而我在postgres用户执行命令?

感谢 干杯,

编辑 我更改命令以

sudo -u postgres for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" demodoo` ;do psql -c "alter table $tbl owner to postgres" demodoo ;done 

但现在我得到另一个错误,我无法理解的起源:

-bash: syntax error near unexpected token `do' 
+1

检查此链接:http://serverfault.com/questions/601140/whats-the-difference-between-sudo-su-postgres-and-sudo-u-postgres – 2014-11-25 10:46:28

+0

谢谢,我在阅读帖子后编辑了我的问题,但我得到一个错误 – renard 2014-11-25 11:07:42

+0

此链接可能会帮助你:http://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist – 2014-11-25 11:12:01

回答

0

我finnaly让它的工作,通过保存命令的内容

for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" demodoo` ;do psql -c "alter table $tbl owner to postgres" demodoo ;done 
在文件 myfile.sh

,然后调用该文件如下:

sudo -u postgres /bin/bash myfile.sh 

谢谢您的帮助

8

尝试:

sudo -u postgres psql

>CREATE USER root WITH SUPERUSER;