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