HOWTO · PostgreSQL

How to Start PostgreSQL Server on Windows

This article teaches how to start a PostgreSQL server on Windows.

On this page

Today, we will be learning how to start a PostgreSQL server on Windows. In the previous article about downloading and installing PostgreSQL on Windows, we already learned how to start a session, create a USER, issue queries, and work with different database objects.

Our topic now focuses on trying to run a PostgreSQL server session to which other users in a local system can connect.

Basic PG_CTL Initialization Commands in Windows

pg_ctl is the command-line utility used to initialize, start, stop, and restart a PostgreSQL server. It gives you direct control over a server instance: you point it at a data folder, and it starts or stops the server that uses that folder.

To start a server on Windows, open a command prompt and run pg_ctl from the bin folder of your PostgreSQL installation (or from anywhere, if that folder is on your PATH), pointing -D at the data folder:

pg_ctl start -D "C:\Program Files\PostgreSQL\14\data"

This will start your PostgreSQL server on the default port, 5432. To run it on a different port on your computer, on which you want other users to listen, pass the port to the server through the -o option:

pg_ctl -o "-p 5656" start -D "C:\Program Files\PostgreSQL\14\data"

Replace 5656 with the port number you want, without any brackets. This will help start your server and allow other users to connect to it on different systems. However, if your PostgreSQL server stopped for some reason, you are better off using the restart command, which stops the server if it is running and then starts it again:

pg_ctl restart -D "C:\Program Files\PostgreSQL\14\data"

Many times, there will be no need to do any of this. The standard PostgreSQL installer for Windows registers the server as a Windows service that starts automatically when the PC boots, so a server is usually already running. pgAdmin and psql are clients - they connect to a running server; they do not start one.

Specify Paths While Issuing PG_CTL START/RESTART in Windows

Sometimes, calling a simple pg_ctl start/restart isn’t the way to go. You may need to specify the DATA folder path within your PostgreSQL installation with -D and then issue the boot commands, as in the examples above.

If a server is already running on the same data folder, a second start attempt will produce an output like this:

Output:

pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2022-04-28 19:28:38.766 PKT [3228] LOG:  redirecting log output to logging collector process
2022-04-28 19:28:38.766 PKT [3228] HINT:  Future log output will appear in directory "log".
 stopped waiting
pg_ctl: could not start server
Examine the log output.

The output above happens when a PostgreSQL server is already running. Because the service started at boot was already up in the background, calling the command above could not initialize a second server on the same data folder. However, doing the following would run a separate server on the same PC: it needs a data folder of its own - a data folder can only ever be used by one running server - initialized once with initdb, and a different port:

pg_ctl -o "-p 5656" start -D "C:\Program Files\PostgreSQL\14\data2"

Output:

waiting for server to start....2022-04-28 19:34:04.588 PKT [11784] LOG:  redirecting log output to logging collector process
2022-04-28 19:34:04.588 PKT [11784] HINT:  Future log output will appear in directory "log".
 done
server started

Similarly, you could also issue the same commands for RESTART.

pg_ctl restart -D "C:\Program Files\PostgreSQL\14\data"

or

pg_ctl -o "-p 5656" restart -D "C:\Program Files\PostgreSQL\14\data"

Output:

waiting for server to shut down.... done
server stopped
waiting for server to start....2022-04-28 19:35:46.007 PKT [884] LOG:  redirecting log output to logging collector process
2022-04-28 19:35:46.007 PKT [884] HINT:  Future log output will appear in directory "log".
 done
server started

You can see in the output how the server is first shut down because it is already running, then it is restarted and logged for any changes during its session.

Use SERVICES.MSC to START/STOP a PostgreSQL Session in Windows

Because the installer registers PostgreSQL as a Windows service, we can control it in SERVICES.MSC, the Windows Services Manager. For STOPPED or PAUSED services, this is often the simplest route.

  1. Press Windows+R and type in SERVICES.MSC.

  2. Once the Windows Services Manager has opened, search for the PostgreSQL server service in the list. It may be named as follows: postgresql-x64-14 - PostgreSQL Server 14.

    Windows Services Manager

  3. Right-click on this to either START, STOP, PAUSE, RESUME, or RESTART your server. You can even change the startup type to AUTOMATIC for it to start by itself the next time.

The Services Manager also tends to tell us the cause of the failure of a specific service. To view the executable path, you can open the General tab and view it as follows.

General tab

Initiate the PostgreSQL Server From the COMMAND PROMPT Using NET START

We can use the NET START command to start a service from the command prompt. The syntax is as follows.

NET START [service_name]

You may need to find the SERVICE_NAME for your PostgreSQL service. It is also mentioned in the General tab.

General service name

Now you can issue a command as follows.

net start postgresql-x64-14

This will boot up our server and display output as follows.

Output:

The postgresql-x64-14 - PostgreSQL Server 14 service is starting.
The postgresql-x64-14 - PostgreSQL Server 14 service was started successfully.

However, remember that COMMAND PROMPT needs to be started as an ADMINISTRATOR and not a normal user. Right-click on the CMD.EXE executable file and click Run as Administrator. Else you will get an error as follows.

Output:

System error 5 has occurred.

Access is denied.

So today, we learned how to start a PostgreSQL server on Windows in different ways. We hope you read through this article carefully and can use it per your needs.