How to Start PostgreSQL Server on Windows
-
Basic
PG_CTL
Initialization Commands in Windows -
Use
SERVICES.MSC
toSTART/STOP
a PostgreSQL Session in Windows
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 a utility used to start and control a PostgreSQL server. PG_CTL
is a manual setup for initializing a PostgreSQL session as it provides a controlled exit, encapsulates different tasks, and can be used for various operations related to our database.
To start a server on Windows, go to your command prompt inside the PostgreSQL installation for the BIN
folder.
pg_ctl start -D "[\data folder directory]";
This will start your PostgreSQL server. To do this on a different port on your computer on which you want other users to listen, use a query as follows.
pg_ctl -o "-F -p [post_number]" start
Enter your PORT NUMBER
in the port_number
defined without the brackets. This will help start your server and allow other users to connect to it on different systems.
However, if your PostgreSQL server stopped due to a reason or a slight malfunction, you are better off using any of the following commands to make it work.
pg_ctl restart -D "[\data folder];
pg_ctl start company; --may or may not work / may produce errors
...
Many times, there will be no need to do any of this. Starting PGADMIN
, for example, tends to already create a PostgreSQL session on a pre-defined port in your PC
, allowing other users to discover it easily.
There may be no need to write any of the commands given above to make your PostgreSQL server boot up.
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 and then issue the boot commands.
Calling the queries given above, you will get an OUTPUT
.
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 given above happens when a PostgreSQL server is already running. Because we already had PGADMIN
booted up and running in the background, calling the query above would be unable to initialize a server on the port already being used.
However, doing something below would run separate servers on the same PC.
pg_ctl -o "-F -p 5656" start -D "C:\Program Files\PostgreSQL\14\data"
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 "-F -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
We already know that a PostgreSQL server is instantiated on your PC
when you launch it, either from PSQL
or PGADMIN
. We can do a few things in SERVICES.MSC
For STOPPED
or PAUSED
services, the Windows Services Manager.
-
Press Windows+R and type in
SERVICES.MSC
. -
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
. -
Right-click on this to either
START
,STOP
,PAUSE
,RESUME
, orRESTART
your server. You can even change the startup time toAUTOMATIC
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.
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 session. It is also mentioned in the General
tab.
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.
Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!
GitHub