Installing PostgreSQL for development and testing on Microsoft Windows
Prerequesites
- Windows 10 build 16299 or later
- An account with Administrator rights
Installing
For a development machine, we'll use WinGet to download and install PostgreSQL:
...where 16
is the major version of PostgreSQL we wish to install. This will install PostgreSQL in unattended mode (it won't ask you questions on what components to install, where to install them, or what the initial configuration should look like... Although you may be prompted by Windows to approve admin access for the installer). The installer will use the default location (C:\Program Files\PostgreSQL\
) with the default password (postgres
) and also install both StackBuilder and pgAdmin.
Tip
You can find a list of available PostgreSQL versions by using WinGet's search
command:
Installers for release candidate versions of PostgreSQL are also available on EDB's website at https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
Further reading
For more control over installation (specifying components, location, port, superuser password...), you can also download and run the installer interactively by following the instructions in Installing PostgreSQL on Windows.
Add PostgreSQL commands to your path
PostgreSQL comes with several useful command-line tools for working with your databases. For convenience, you'll probably want to add their location to your path.
- Open the System Properties control panel and select the Advanced tab (or run
SystemPropertiesAdvanced.exe
) - Activate the "Environment Variables..." button to open the envornment variables editor
- Find the
Path
variable under the "System variables" heading, and click "Edit..." - Add the path to the bin directory of the PostgreSQL version you installed, e.g.
C:\Program Files\postgresql\16\bin\
(where 16 is the version of PostgreSQL that you installed).
This only affects new command prompts
If you have a command prompt open already, you'll need to close and reopen in order for your changes to the system path to take effect.
Verifying your installation
If the steps above completed successfully, you'll now have a PostgreSQL service running with the default database and superuser account. Let's verify this by connecting, creating a new user and database, and then connecting using that user.
Connect with psql
Open a new command prompt, and run
When prompted, enter the default password (postgres
).
Warning
We're setting up an environment for local development, and have no intention of enabling remote connections or working with sensitive data - so leaving the default password is fine. Never leave the default password set on a production or multi-user system!
It's a good practice to develop with a user and database other than the default (postgres
database and postgres
superuser) - this allows us to apply the principle of least privilege and helps avoid issues later on when you go to deploy: since the superuser is allowed to modify anything, you'll never encounter permissions errors even if your app's configuration has you reading or writing to the wrong table, schema or database... Yet that's certainly not the sort of bug you'd wish to ship! So let's start off right by creating an app user and giving it its own database to operate on:
Further reading
Now let's quit psql and connect with pgAdmin as our new user. Run,
Connect with pgAdmin
Launch pgAdmin via the Start menu (you can find it under "PostgreSQL 16", or just type "pgAdmin").
If you poke around a bit (by expanding the Servers group on the left), you'll see that pgAdmin comes with a default connection configured for the postgres
user to the postgres
database in our local PostgreSQL server.
Let's add a new connection for our app user to our app database.
Right-click the Servers entry on the left, select Register and click Server:
On the General tab, give it a descriptive name like "myapp db"
Switch to the Connection tab:
enter connection information, using the new role and database we created in psql above:
localhost
for Host name/addressappdb
for Maintenance databasemyapp
for Usernameapp-password
for Password
...and check the Save password? option and click the Save button.
Note that some areas that require system-level permissions (e.g., logs) are unavailable, as you're not connected as a superuser. For these, you can use the default superuser connection.
Conclusion
By following these steps, you've created a local environment for developing against PostgreSQL. You've created your own database and limited-access user to own it, and can proceed to create a schema, connect application frameworks, run tests, etc.
Could this page be better? Report a problem or suggest an addition!