Windows: How to run PostgreSQL without installation (portable)
Introduction
Running a PostgreSQL database server on Windows is usually done by installing the PostgreSQL server using the official installer. However, there are many scenarios where you might want to run a PostgreSQL server without installing it in your system.
In this post, I'll show you how to run a PostgreSQL server on Windows without installation and how to manage it as a Windows service.
Downloading PostgreSQL
The first step is to download the PostgreSQL binaries. In general, PostgeSQL can be downloaded from the postgresql.org website. However, the official community website only hosts the installer for Windows. The binary-only packages are only available from the commercial EnterpriseDB website.
You can download the PostgreSQL binaries from the EnterpriseDB website.
Extracting the PostgreSQL binaries
The next step is extracting the downloaded archive into a directory of your choice. Notice that this is the directory where the PostgreSQL server will be running from.
Initializing the PostgreSQL database
Before starting the PostgreSQL server, you need to initialize the database. To do this, open a command prompt and navigate to the directory where you extracted the PostgreSQL binaries.
Run the following command to initialize the database:
.\bin\initdb.exe -D data -U postgres -W -E UTF8 -A scram-sha-256
The following table describes the parameters used in the initdb
command:
Parameter | Description |
---|---|
-D data | Specifies the directory where the database cluster will be created. In this case .\data . |
-U postgres | Specifies the user that will own the database cluster. In this case postgres . |
-W | Prompts for the password of the user specified with the -U parameter. |
-E UTF8 | Specifies the encoding of the database. In this case UTF8 . |
-A scram-sha-256 | Specifies the authentication method for the database. In this case password authentication. |
In this case, I've opted to create the data directory in the same directory where the PostgreSQL binaries were extracted. However, you can specify any directory you want, even in a different drive.
Starting the PostgreSQL server
To start the PostgreSQL server, run the following command:
.\bin\pg_ctl.exe start -D data
The command will start the PostgreSQL server in the background and print server started
when the server is ready.
Stopping the PostgreSQL server
To shut down the PostgreSQL server, run the following command:
.\bin\pg_ctl.exe stop -D data
When the server stops, it will print server stopped
.
Running PostgreSQL as a Windows service
Administrative privileges are required to run the following commands.
You can optionally run the PostgreSQL server as a Windows service. To do this, you need to install the service by running the following command:
.\bin\pg_ctl.exe register -N PostgreSQL -D data
The command will install the PostgreSQL server as a Windows service named PostgreSQL
.
To remove the service, you can run the following command:
.\bin\pg_ctl.exe unregister -N PostgreSQL
Note that you need to specify the same name you used when installing the service.
Additional tools
In addition to the PostgreSQL server binaries, the archive also contains the psql
command-line tool and the pgAdmin
graphical user interface.
The psql
command-line tool can be used to interact with the PostgreSQL server from the command line.
You can find it in the .\bin
directory.
The pgAdmin
graphical user interface can be used to manage the PostgreSQL server in a more user-friendly way.
You can find it in the .\pgAdmin
directory.