/ Bit by bit / posts

MSDOS Script to Set Up PostgreSQL

May 23, 2020

Don’t ask “Why Windows?”

Not being a database expert and needing to set up PostgreSQL database in a Windows machine to do simple development with minimal fuss, the following is a simplistic MSDOS script to automate that:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@ECHO OFF
@BREAK ON
@SETLOCAL

SET PG_HOME=path_to_pg_home
SET superuser=postgres

IF NOT DEFINED PGDATA GOTO :MISSINGPGDATA
IF EXIST %PGDATA% GOTO :DBEXISTS

SET PGBIN=%PG_HOME%\bin
ECHO Initialzing database with superuser name: %superuser%

%PGBIN%\initdb -D %PGDATA% --pwprompt --username=%superuser% ^
  --no-locale --encoding=UTF8 ^
  --auth-local=trust
  --auth-host=trust

GOTO :EOF

:MISSINGPGDATA
ECHO Please set the environment variable PGDATA before running again.
GOTO :EOF

:DBEXISTS
ECHO Database already initialized at %PGDATA%. Please specify a different %%PGDATA%%.

A few things to note:

Once the initializing is completed, run pg_ctl start -D %PGDATA% -l logfile to start the database daemon. To stop it, run pg_ctl stop.