美文网首页
Windows系统中使用PostgreSQL命令批量新建数据库d

Windows系统中使用PostgreSQL命令批量新建数据库d

作者: 羋学僧 | 来源:发表于2024-04-25 10:13 被阅读0次

    Windows系统中使用PostgreSQL命令批量新建数据库database

    PSQL_Create.bat
    @echo off
    
    title PSQL_Create
    mkdir logs
    set LOG_FILE=logs/PSQL_Create.log
    set PGPASSWORD=123456798
    set PGUSER=postgres
    set PGDATABASE1=database1
    set PGHOST=127.0.0.1
    set PGPORT=5432
    
    echo. > %LOG_FILE%
    
    psql -U %PGUSER%  -h %PGHOST% -p %PGPORT% -c "SELECT 1 FROM pg_database WHERE datname=%PGDATABASE1%" > nul 2>&1
    
    if %errorlevel% equ 0 (
        echo "%PGDATABASE1%数据库已存在" >> "%LOG_FILE%"
    ) else (
        psql -U %PGUSER%  -h %PGHOST% -p %PGPORT% -c "CREATE DATABASE %PGDATABASE1%;"
        echo "%PGDATABASE1%数据库已创建" >> "%LOG_FILE%"
    )
    
    set PGDATABASE2=database2
    
    psql -U %PGUSER%  -h %PGHOST% -p %PGPORT% -c "SELECT 1 FROM pg_database WHERE datname=%PGDATABASE2%" > nul 2>&1
    
    if %errorlevel% equ 0 (
        echo "%PGDATABASE2%数据库已存在" >> "%LOG_FILE%"
    ) else (
        psql -U %PGUSER%  -h %PGHOST% -p %PGPORT% -c "CREATE DATABASE %PGDATABASE2%;"
        echo "%PGDATABASE2%数据库已创建" >> "%LOG_FILE%"
    ) 
    pause
    exit
    

    相关文章

      网友评论

          本文标题:Windows系统中使用PostgreSQL命令批量新建数据库d

          本文链接:https://www.haomeiwen.com/subject/oehnxjtx.html