Skip to content

Instantly share code, notes, and snippets.

@RobbiNespu
Created May 13, 2022 03:38
Show Gist options
  • Save RobbiNespu/90e2aa3de4ce0b722dda8c5e7c8576d6 to your computer and use it in GitHub Desktop.
Save RobbiNespu/90e2aa3de4ce0b722dda8c5e7c8576d6 to your computer and use it in GitHub Desktop.
Portable postgre SQL on windows
@ECHO ON
:: Save the directory where we are
pushd %CD%
echo %CD%
:: Shift directories to the correct path
%~d0
cd %~dp0
:: Set the variable using the common file
@SET PATH="%~dp0\pgsql\bin";%PATH%
@SET PGDIR=%~dp0\pgsql
@SET PGDATA=%PGDIR%\data
@SET PGLOCALEDIR=%PGDIR%\share\locale
@SET PGPORT=5412
@SET PGDATABASE=postgres
@SET PGUSER=postgres
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
@SET PGLOGS=%~dp0\logfile_%fullstamp%
:: The actual command that needs to do something
::"%PGDIR%\bin\pg_ctl" <---- the actual parameters: start, stop, initdb ----->
"%PGDIR%\bin\initdb" -U postgres -A trust -E UTF8
:: Restore the directory where we started from
popd
pause
@ECHO ON
:: Save the directory where we are
pushd %CD%
echo %CD%
:: Shift directories to the correct path
%~d0
cd %~dp0
:: Set the variable using the common file
::call vars.cmd
@SET PATH="%~dp0\pgsql\bin";%PATH%
@SET PGDIR=%~dp0\pgsql
@SET PGDATA=%PGDIR%\data
@SET PGLOCALEDIR=%PGDIR%\share\locale
@SET PGPORT=5412
@SET PGDATABASE=postgres
@SET PGUSER=postgres
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
@SET PGLOGS=%~dp0\logfile_%fullstamp%
:: The actual command that needs to do something
::"%PGDIR%\bin\pg_ctl" <---- the actual parameters: start, stop, initdb ----->
"%PGDIR%\bin\pg_ctl" -D "%PGDATA%" -l "%PGLOGS%" start
:: Restore the directory where we started from
popd
set pg
echo "execute stop script and press enter here to kill the bash"
pause
@ECHO ON
:: Save the directory where we are
pushd %CD%
echo %CD%
:: Shift directories to the correct path
%~d0
cd %~dp0
:: Set the variable using the common file
:: call pg12-vars.cmd
@SET PATH="%~dp0\pgsql\bin";%PATH%
@SET PGDIR=%~dp0\pgsql
@SET PGDATA=%PGDIR%\data
@SET PGLOCALEDIR=%PGDIR%\share\locale
@SET PGPORT=5412
@SET PGDATABASE=postgres
@SET PGUSER=postgres
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
@SET PGLOGS=%~dp0\logfile_%fullstamp%
:: The actual command that needs to do something
::"%PGDIR%\bin\pg_ctl" <---- the actual parameters: start, stop, initdb ----->
"%PGDIR%\bin\pg_ctl" -D "%PGDATA%" -l "%PGLOGS%" stop
:: Restore the directory where we started from
popd
pause

My dir structures:

X:\OneDrive\WareZ\database\postgresql\postgresql-14.2-2-windows-x64-binaries>dir
 Volume in drive X is <redacted>
 Volume Serial Number is <redacted>

 Directory of X:\OneDrive\WareZ\database\postgresql\postgresql-14.2-2-windows-x64-binaries

13/05/2022  11:24 AM    <DIR>          .
13/05/2022  11:24 AM    <DIR>          ..
13/05/2022  11:24 AM             1,012 1-init.cmd
13/05/2022  11:22 AM             1,110 2-start.cmd
13/05/2022  11:05 AM               671 3-stop.cmd
13/05/2022  11:23 AM               882 logfile_2022-05-13_11-23-10
13/05/2022  11:25 AM               876 logfile_2022-05-13_11-24-57
13/05/2022  11:13 AM               221 vars.cmd
13/05/2022  10:58 AM    <DIR>          pgsql
               6 File(s)          4,772 bytes
               3 Dir(s)  175,228,665,856 bytes free
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment