PG创建只读用户

方法一

create user test_user  password 'test_password';
revoke usage on schema public from test_user;

根据实际情况,对用户名与密码进行修改
可以将该代码封装成一个shell脚本

方法二

  • 先创建函数
--create readonly user.sql

CREATE OR REPLACE FUNCTION func_create_user(user_name varchar, user_password varchar) 
RETURNS INTEGER 
AS
$BODY$
DECLARE
	sql varchar:='';
BEGIN
	sql=format('create user %s with password ''%s''',user_name, user_password);
	execute 'revoke create on schema public from public;';
	execute sql;
    RETURN 0;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
  • 然后通过shell脚本调用该函数
#!/bin/sh
function export_dbpasswd()
{
	  export PGUSER=postgres
      export PGPASSWORD=passwd
}
export_dbpasswd

/home/postgres/pgsql/bin/psql -Upostgres -d postgres  -f create_user.sql
/home/postgres/pgsql/bin/psql -Upostgres -d postgres  -c "select * from func_create_user('$1'::VARCHAR, '$2'::VARCHAR);"


if [[ $? -eq 0 ]];then
	echo "create readonly user $1 successfully"
fi

#参考:
https://blog.csdn.net/lk_db/article/details/78246205
http://www.oracleonlinux.cn/2017/04/postgresql_grant_revoke_priviledges/

posted @ 2019-09-22 12:06  岳麓丹枫  阅读(270)  评论(0编辑  收藏  举报