首页 > 数据库 >【PG】创建数据库,账户以及schema

【PG】创建数据库,账户以及schema

时间:2024-03-14 22:16:20浏览次数:27  
标签:account name 数据库 db echo PG conf pg schema


#!/bin/bash

# Check if running as the postgres user
if [[ "$USER" != "postgres" ]]; then
  echo "Error: This script must be run as the postgres user."
  exit 1
fi

# Check input parameters
if [ $# -ne 3 ]; then
  echo "Error: Incorrect number of parameters."
  echo "Usage: $0 <database_name> <account_name> <schema_name>"
  exit 1
fi

# Extract input parameters
db_name=$1
account_name=$2
schema_name=$3

# Drop the database
echo "Dropping the database $db_name"
psql -c "DROP DATABASE IF EXISTS $db_name;"

# Drop the account
echo "Dropping the account $account_name"
psql -c "DROP USER IF EXISTS $account_name;"

# Create the database
echo "Creating the database $db_name"
psql -c "CREATE DATABASE $db_name;"

# Create the account
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 " "

# Switch to the target database
echo "Switching to the database $db_name"
psql -d $db_name -c "CREATE SCHEMA $schema_name;"

echo " "
# Backup pg_hba.conf
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"

# Add entries to pg_hba.conf
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 " "
# Reload PostgreSQL configuration
echo "Reloading PostgreSQL configuration"
psql -c "SELECT pg_reload_conf();"

echo "Operation completed."

echo " "



echo "Operation completed."

标签:account,name,数据库,db,echo,PG,conf,pg,schema
From: https://www.cnblogs.com/Jeona/p/18074115

相关文章

  • 【PG】ora2pg 分别导出表,索引,存储过程等
    #!/bin/bash#检查ora2pg命令是否可用command-vora2pg>/dev/null2>&1||{echo>&2"ora2pg工具未安装或未在PATH中。请先安装并配置好ora2pg工具。";exit1;}#配置文件路径ora2pg_conf="/path/to/ora2pg.conf"#导出表结构和数据echo"导出表结构和数据..."......
  • 【PG】对比两个数据中对象
    #!/bin/bash####################################################################################################################################Name:Compare2PostgresDBs##Description:Compare2PostgresDBsTables,Indexes,andFunctions.Incas......
  • 【PG】不同PG中迁移表
    #!/bin/bash#SourcedatabasecredentialsSRC_DB_HOST="localhost"SRC_DB_PORT="5442"SRC_DB_NAME="postgres"SRC_DB_USER="myuser"SRC_DB_PASS='mypwd'#DestinationdatabasecredentialsDEST_DB_HOST="l......
  • 【PG】列出表以及大小
    ThispostdemonstratesanexampleofaBashscriptthatconnectstoaDBandprintsalistoftables,theirrecordsnumber,andsize.Theresultsetissortedindescendingorderbysizeandthenbynumberofrecords.Inaddition,theoutputofthescript......
  • 【PG】查询数据大小
    #!/bin/bash##################################################################calculate_a_pure_size_of_the_postgres_db.sh##ThisscriptcalculatesapuresizeofthePostgresDB##DB_Pure_Size=DB_Size-DB_Tables_Bloat-DB_Indexes_Bloat##Aut......
  • 【PG】查看PG对象大小增长情况
    #!/bin/bash####################################################check_postgresql_db_table_rowsnum_and_sizing.sh##Thisscriptsiteratesartidatabasesand#populatessometargetdbtablewith#infoabouttablesrownumandsizing##Date:10-Aug-......
  • 数据库不应放在容器中?- B站Kubernetes有状态服务实践(Elasticsearch/Clickhouse)
    本期作者前言云原生时代下,Kubernetes已成为容器技术的事实标准, 使得基础设施领域应用下自动化运维管理与编排成为可能。对于无状态服务而言, 业界早已落地数套成熟且较完美的解决方案。可对于有状态的服务, 方案的复杂度就以几何倍数增长, 例如分布式应用多个实例间的依......
  • 【SpringBoot】自定义工具类实现Excel数据新建表存入MySQL数据库
    ......
  • 基于大模型和向量数据库的 RAG 示例
    1        RAG介绍RAG是一种先进的自然语言处理方法,它结合了信息检索和文本生成技术,用于提高问答系统、聊天机器人等应用的性能。2        RAG的工作流程文档加载(DocumentLoading)从各种来源加载大量文档数据。这些文档将作为知识库,用于后续的信息检索......
  • 【鸿蒙ArkTs】沙箱内sqlite数据库db文件导入导出
    1.数据库在沙箱内位置沙箱文件目录官方文档说明获取数据库路径:let数据库路径='/data/storage/el2/database/entry/rdb/Mydata.db'2.数据库临时文件找到了数据库db文件,只导出这个db文件是没有用的。因为还有db-wal这个临时文件。系统并没有把数据全部写入db,甚至重启手机......