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.
- 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.
- Get binaries for Windows.
- Next copy the below batch file into the root of the unzipped postgresql folder, where you can find these folders: bin, include, lib
- Run the batch file
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