11:: --------------------------------------------------------------------------------
2- :: OPEN SERVER PANEL | DB INIT SCRIPT
2+ :: OPEN SERVER PANEL | DB INIT SCRIPT (robust for CI)
33:: --------------------------------------------------------------------------------
44@ echo off
5+ setlocal EnableExtensions
56set LC_MESSAGES = English
7+
8+ :: Resolve root paths
69set " TMP_ROOT = %~dp0 .."
710for %%I in (" %TMP_ROOT% " ) do set " TMP_ROOT = %%~fI "
811set " OSP_ROOT_DIR = %TMP_ROOT% "
912set " OSP_ROOT_DIR_UNIX = %TMP_ROOT:\ =/ % "
10- chcp 65001
13+
14+ :: UTF-8 codepage
15+ chcp 65001 > nul
16+
1117TITLE DB Generator
18+
19+ :: ------------------------------------------------------------------------------
20+ :: Run MySQL/MariaDB generators (оставлены как есть)
21+ :: ------------------------------------------------------------------------------
1222call " %OSP_ROOT_DIR% \generate\genmariadb.bat"
1323call " %OSP_ROOT_DIR% \generate\genmysql.bat"
14- call :posgresql PostgreSQL-11
15- call :posgresql PostgreSQL-12
16- call :posgresql PostgreSQL-13
17- call :posgresql PostgreSQL-14
18- call :posgresql PostgreSQL-15
19- call :posgresql PostgreSQL-16
20- call :posgresql PostgreSQL-17
24+
25+ :: ------------------------------------------------------------------------------
26+ :: PostgreSQL init per version
27+ :: ------------------------------------------------------------------------------
28+ call :postgresql PostgreSQL-11
29+ call :postgresql PostgreSQL-12
30+ call :postgresql PostgreSQL-13
31+ call :postgresql PostgreSQL-14
32+ call :postgresql PostgreSQL-15
33+ call :postgresql PostgreSQL-16
34+ call :postgresql PostgreSQL-17
35+
2136goto end
37+
2238:: --------------------------------------------------------------------------------
23- :: INIT PostgreSQL
39+ :: INIT PostgreSQL (robust, no PowerShell, safe dirs, clean start)
40+ :: Args: %1 = module name, e.g. PostgreSQL-15
2441:: --------------------------------------------------------------------------------
25- :posgresql
26- setlocal
27- powershell -NoLogo -NoProfile -Command ^
28- " try { (Get-Content '%OSP_ROOT_DIR% \generate\config\PostgreSQL\postgresql.conf') -replace '{root_dir}', '%OSP_ROOT_DIR% ' -replace '{module_name}', '%1 ' | Set-Content '%OSP_ROOT_DIR% \modules\%1 \ospanel_data\default_data\postgresql.conf'; exit 0 } catch { exit 1 }"
29- copy /Y " %OSP_ROOT_DIR% \generate\config\PostgreSQL\pg_hba.conf" " %OSP_ROOT_DIR% \modules\%1 \ospanel_data\default_data\pg_hba.conf"
30- set " PGDATA = %OSP_ROOT_DIR% \modules\%1 \ospanel_data\default_data"
42+ :postgresql
43+ setlocal EnableDelayedExpansion
44+
45+ set " MODULE = %~1 "
46+ set " DB_DIR = %OSP_ROOT_DIR% \modules\%MODULE% "
47+ set " DATA_DIR = %DB_DIR% \ospanel_data\default_data"
48+ set " TMP_DIR = %DB_DIR% \temp"
49+ set " BIN_DIR = %DB_DIR% \bin"
50+
51+ :: Проверки наличия каталога модуля и bin
52+ if not exist " %DB_DIR% " (
53+ echo [%date% %time% ] ❌ ERROR: Module directory not found: " %DB_DIR% "
54+ endlocal & exit /b 1
55+ )
56+ if not exist " %BIN_DIR% \initdb.exe" (
57+ echo [%date% %time% ] ❌ ERROR: initdb.exe not found: " %BIN_DIR% \initdb.exe"
58+ endlocal & exit /b 1
59+ )
60+
61+ :: Чистый старт: удалить старые data/temp и создать заново
62+ if exist " %TMP_DIR% " rd /s /q " %TMP_DIR% "
63+ if exist " %DATA_DIR% " rd /s /q " %DATA_DIR% "
64+
65+ mkdir " %TMP_DIR% " 2 > nul
66+ mkdir " %DATA_DIR% " 2 > nul
67+
68+ if not exist " %DATA_DIR% " (
69+ echo [%date% %time% ] ❌ ERROR: Failed to create data dir: " %DATA_DIR% "
70+ endlocal & exit /b 1
71+ )
72+
73+ :: Подготовка шаблонов конфигурации
74+ set " SRC_CONF = %OSP_ROOT_DIR% \generate\config\PostgreSQL\postgresql.conf"
75+ set " SRC_HBA = %OSP_ROOT_DIR% \generate\config\PostgreSQL\pg_hba.conf"
76+
77+ :: Если есть шаблоны — скопируем их в data (initdb позже перезапишет своими дефолтами некоторые опции,
78+ :: мы заменим файлы после initdb ещё раз при необходимости).
79+ if exist " %SRC_CONF% " (
80+ copy /Y " %SRC_CONF% " " %DATA_DIR% \postgresql.conf" > nul
81+ )
82+ if exist " %SRC_HBA% " (
83+ copy /Y " %SRC_HBA% " " %DATA_DIR% \pg_hba.conf" > nul
84+ )
85+
86+ :: Подстановка плейсхолдеров в конфиги (если файлы существуют)
87+ :: В конфиге обычно удобнее использовать прямые слэши:
88+ set " ROOT_DIR_ESC = %OSP_ROOT_DIR:\ =/ % "
89+ for %%F in (" postgresql.conf" " pg_hba.conf" ) do (
90+ if exist " %DATA_DIR% \%%~F " (
91+ set " TMPF = %DATA_DIR% \%%~nF .tmp"
92+ > " !TMPF! " (
93+ for /f " usebackq delims=" %%L in (" %DATA_DIR% \%%~F " ) do (
94+ set " line = %%L "
95+ set " line = !line:{root_dir} =%ROOT_DIR_ESC% ! "
96+ set " line = !line:{module_name} =%MODULE% ! "
97+ echo(!line!
98+ )
99+ )
100+ move /y " !TMPF! " " %DATA_DIR% \%%~F " > nul
101+ )
102+ )
103+
104+ :: Переменные окружения для initdb/psql
105+ set " PGDATA = %DATA_DIR% "
31106set " PGCLIENTENCODING = utf-8"
32107set " PGHOST = 127.0.0.1"
33- set " PGLOCALEDIR = %OSP_ROOT_DIR% \modules\%1 \share\locale"
34- set " PGPORT = 5432"
108+ set " PGLOCALEDIR = %DB_DIR% \share\locale"
35109set " PGSSLMODE = disable"
36- set " PGSYSCONFDIR = %OSP_ROOT_DIR% \modules\ %1 \ospanel_data\default_data "
110+ set " PGSYSCONFDIR = %DATA_DIR% "
37111set " PGTZ = {time_zone}"
38112set " PGUSER = postgres"
39- set " TEMP = %OSP_ROOT_DIR% \modules\%1 \temp"
40- set " TMP = %OSP_ROOT_DIR% \modules\%1 \temp"
41- rd " %TMP% " /s /q
42- rd " %PGDATA% " /s /q
43- mkdir " %TMP% "
44- mkdir " %PGDATA% "
45- %OSP_ROOT_DIR% \modules\%1 \bin\initdb.exe --data-checksums --no-locale -U postgres --encoding=UTF8 -D " %PGDATA% "
46- del " %PGDATA% \pg_hba.conf" " %PGDATA% \postgresql.conf"
47- rd /s /q " %TMP% "
113+
114+ :: Назначение порта (если когда-либо понадобится запускать сервер в этом же скрипте)
115+ :: По умолчанию 5432. Можно разнести по версиям:
116+ call :_pg_port_for_version " %MODULE% " PGPORT
117+ set " PGPORT = %PGPORT% "
118+
119+ :: Выполняем initdb (data dir уже создан)
120+ pushd " %DB_DIR% "
121+ " %BIN_DIR% \initdb.exe" --data-checksums --no-locale -U postgres --encoding=UTF8 -D " %PGDATA% "
122+ set " ec = !errorlevel! "
123+ popd
124+
125+ if not " !ec! " == " 0" (
126+ echo [%date% %time% ] ❌ ERROR: initdb failed for %MODULE% (code !ec! )
127+ :: Не чистим data, чтобы можно было посмотреть логи, если они появились
128+ endlocal & exit /b 1
129+ )
130+
131+ :: После initdb: у initdb создаются собственные postgresql.conf/pg_hba.conf.
132+ :: Если нужно принудительно заменить на наши шаблоны ещё раз — раскомментируйте:
133+ :: if exist "%SRC_CONF%" copy /Y "%SRC_CONF%" "%DATA_DIR%\postgresql.conf" >nul
134+ :: if exist "%SRC_HBA%" copy /Y "%SRC_HBA%" "%DATA_DIR%\pg_hba.conf" >nul
135+ :: и повторите подстановку плейсхолдеров:
136+ :: for %%F in ("postgresql.conf" "pg_hba.conf") do ( ... )
137+
138+ :: Удаляем временный каталог
139+ if exist " %TMP_DIR% " rd /s /q " %TMP_DIR% "
140+
141+ echo [%date% %time% ] ✅ PostgreSQL initialized: %MODULE%
48142endlocal
49143exit /b 0
144+
145+ :: --------------------------------------------------------------------------------
146+ :: Helper: map version to port (optional; default 5432)
147+ :: Args: %1 = module (e.g. PostgreSQL-15), %2 = out var name (e.g. PGPORT)
148+ :: --------------------------------------------------------------------------------
149+ :_pg_port_for_version
150+ setlocal
151+ set " MODULE = %~1 "
152+ set " OUTVAR = %~2 "
153+ set " PORT = 5432"
154+
155+ if /i " %MODULE% " == " PostgreSQL-11" set " PORT = 54111"
156+ if /i " %MODULE% " == " PostgreSQL-12" set " PORT = 54121"
157+ if /i " %MODULE% " == " PostgreSQL-13" set " PORT = 54131"
158+ if /i " %MODULE% " == " PostgreSQL-14" set " PORT = 54141"
159+ if /i " %MODULE% " == " PostgreSQL-15" set " PORT = 54151"
160+ if /i " %MODULE% " == " PostgreSQL-16" set " PORT = 54161"
161+ if /i " %MODULE% " == " PostgreSQL-17" set " PORT = 54171"
162+
163+ endlocal & set " %OUTVAR% = %PORT% "
164+ exit /b 0
165+
50166:end
51167echo on
168+ endlocal
0 commit comments