Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save windsting/920872a03df9a6293179252752a56e6a to your computer and use it in GitHub Desktop.
Save windsting/920872a03df9a6293179252752a56e6a to your computer and use it in GitHub Desktop.
Starting Postgresql in Windows without Install

Starting Postgresql in Windows without Install

Problem

This is a question that comes up quite often by windows users, so thought we would share how we normally do it. The question is

Can you run a PostgreSQL server on your windows desktop/server box without having to install anything?

The answer is

Yes and quite easily.

Why would you need to do this

  • you are developing a single user app that you want users to be able to run from anywhere without having to install it first.
  • you aren't allowed to install anything on a user's pc and you also want to package along a database you already have created.
  • you develop on portable WAMP like things, and for some of applications, they need to work in both MySQL and PostgreSQL, so you need an easy way during development to swap one out for the other.
  • you just don't wanna install it in the fragile and fat Windows.

How

  1. Get binaries for Windows.
  2. Next copy the below batch file into the root of the unzipped postgresql folder, where you can find these folders: bin, include, lib
  3. Run the batch file

Script

Below is the script that will start a PostgreSQL server and clicking the Enter key will shut the service down.

You can carry the server on USB device if you want and launch as you wish.

The assumpution of the script is that it's in the root of your unzipped PostgreSQL folder.

The %CD% returns the folder path of current directory and %~dp0 returns folder path of script.

start.bat

@ECHO OFF

REM Put me in the extracted postgres root folder(which contains 
REM a "bin" folder), and run it.

REM The script sets environment variables helpful for PostgreSQL
@SET PATH="%~dp0\bin";%PATH%
@SET PGDATA=%~dp0\data
@SET PGDATABASE=postgres
@SET PGUSER=postgres
@SET PGPORT=5439
@SET PGLOCALEDIR=%~dp0\share\locale

ECHO.
IF EXIST data (
ECHO This instance already initialized.
ECHO.
) ELSE (
ECHO First run, wait for initializing.
"%~dp0\bin\initdb" -U postgres -A trust
ECHO.
)
"%~dp0\bin\pg_ctl" -D "%~dp0/data" -l logfile start
ECHO.
ECHO postgres listening on port %PGPORT%
ECHO.
REM pause
set /p DUMMY=Hit ENTER to continue...
"%~dp0\bin\pg_ctl" -D "%~dp0/data" stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment