User Tools

Site Tools


postgresql

PostgreSQL

This is a collection of PostgreSQL tricks that I encountered and wanted to archive here.

Reset database to original state (initdb) on Windows

rem ### -------------------------------------------------------------  ###
rem ### v v v v v v v v v v v   BEGIN CONFIG  v v v v v v v v v v v v  ###
set "postgres_path=C:\PostgreSQL\9.4"
 
set local_postgres_pass=REPLACEME
set local_superuser=REPLACEME_admin
set local_superuser_pass=REPLACEME
set local_port=5434
rem ### ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^  END CONFIG  ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ###
rem ### -------------------------------------------------------------- ###
 
 
rem ######################################################################
rem ###            Test if we are running as administrator             ###
rem ######################################################################
net session >nul 2>&1
if %errorLevel% == 0 (
    echo Success: Administrative permissions confirmed.
) else (
    echo Failure: Current permissions inadequate. Must be run as administrator. Exiting now...
    goto :abort
)
 
rem ######################################################################
rem ###            Stop local postgresql cluster                       ###
rem ######################################################################
 
net stop postgresql-x64-9.4
 
rem ######################################################################
rem ###            move old data dir                                   ###
rem ######################################################################
 
set "pg_data_path=!postgres_path!\data"
if exist !pg_data_path! (
    set bak_pg_data_path=!postgres_path!\data_%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
    echo !pg_data_path! exists. Moving to !bak_pg_data_path!
    move "!pg_data_path!" "!bak_pg_data_path!"
    mkdir "!pg_data_path!"
)
 
rem ######################################################################
rem ###            reinitialize the database                           ###
rem ######################################################################
 
"!postgres_path!\bin\initdb.exe" -U postgres -E UTF-8 -D !pg_data_path!
 
rem ######################################################################
rem ###            tweak configuration                                 ###
rem ######################################################################
 
echo port=!local_port! >> "!pg_data_path!\postgresql.conf"
 
rem ######################################################################
rem ###           Start local postgresql cluster                       ###
rem ######################################################################
 
net start postgresql-x64-9.4
 
rem ######################################################################
rem ###            create local superuser                              ###
rem ######################################################################
 
"!postgres_path!\bin\psql.exe" --dbname="postgresql://postgres:!local_postgres_pass!@localhost:!local_port!/postgres" -c "CREATE ROLE !local_superuser! LOGIN  PASSWORD '!local_superuser_pass!' SUPERUSER CREATEDB CREATEROLE REPLICATION VALID UNTIL 'infinity';"
 
:abort
echo errorlevel: %ERRORLEVEL%
call :pause_debug
exit %ERRORLEVEL%
postgresql.txt · Last modified: by zertrin