if [[ "$USER" != "postgres" ]]; then
echo "Error: This script must be run as the postgres user."
exit 1
fi
if [ $# -ne 3 ]; then
echo "Error: Incorrect number of parameters."
echo "Usage: $0 <database_name> <account_name> <schema_name>"
exit 1
fi
db_name=$1
account_name=$2
schema_name=$3
echo "Dropping the database $db_name"
psql -c "DROP DATABASE IF EXISTS $db_name;"
echo "Dropping the account $account_name"
psql -c "DROP USER IF EXISTS $account_name;"
echo "Creating the database $db_name"
psql -c "CREATE DATABASE $db_name;"
echo "Creating the account $account_name and granting privileges on the database $db_name"
psql -c "CREATE USER $account_name WITH ENCRYPTED PASSWORD 'password';"
psql -c "GRANT ALL PRIVILEGES ON DATABASE $db_name TO $account_name;"
psql -c "alter DATABASE $db_name owner TO $account_name;"
echo " "
echo "Switching to the database $db_name"
psql -d $db_name -c "CREATE SCHEMA $schema_name;"
echo " "
backup_file="pg_hba.conf_$(date +'%m_%d_%Y_%H%M%S')"
echo "Backing up pg_hba.conf to $backup_file"
cp /etc/postgresql/<version>/main/pg_hba.conf "/etc/postgresql/<version>/main/$backup_file"
echo "Adding entries to pg_hba.conf"
echo "host $db_name $account_name 127.0.0.1/32 scram-sha-256" >> /etc/postgresql/<version>/main/pg_hba.conf
echo "local $db_name $account_name ::1/128 trust" >> /etc/postgresql/<version>/main/pg_hba.conf
echo " "
echo "Reloading PostgreSQL configuration"
psql -c "SELECT pg_reload_conf();"
echo "Operation completed."
echo " "
echo "Operation completed."
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?