How to Restart PostgreSQL: Simple Commands and Tips
To restart
PostgreSQL, use system service commands like sudo systemctl restart postgresql on Linux or restart the PostgreSQL service via the Services app on Windows. Restarting stops and starts the database server to apply changes or fix issues.Syntax
Restarting PostgreSQL depends on your operating system and how PostgreSQL was installed. Common commands include:
sudo systemctl restart postgresql- Restarts PostgreSQL using systemd on Linux.sudo service postgresql restart- Older Linux systems using SysV init.- Restarting the PostgreSQL service from the Windows Services app.
These commands stop the PostgreSQL server and start it again immediately.
bash
sudo systemctl restart postgresql
Example
This example shows how to restart PostgreSQL on a Linux system using systemd. It stops the server and starts it again, applying any configuration changes.
bash
sudo systemctl restart postgresql sudo systemctl status postgresql
Output
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-06-20 10:00:00 UTC; 5s ago
Main PID: 1234 (postgres)
Tasks: 7 (limit: 4915)
Memory: 20.0M
CGroup: /system.slice/postgresql.service
├─1234 /usr/lib/postgresql/14/bin/postgres -D /var/lib/postgresql/14/main
└─1235 postgres: checkpointer process
Common Pitfalls
Common mistakes when restarting PostgreSQL include:
- Not using
sudoor administrator rights, causing permission errors. - Using the wrong service name if multiple PostgreSQL versions are installed (e.g.,
postgresql@14-main). - Restarting without saving configuration changes, so changes do not take effect.
- On Windows, trying to restart from the command line without proper privileges.
Always check the service status after restarting to confirm it is running.
bash
sudo systemctl restart postgresql@14-main sudo systemctl status postgresql@14-main
Output
● postgresql@14-main.service - PostgreSQL Cluster 14-main
Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-06-20 10:05:00 UTC; 3s ago
Main PID: 2345 (postgres)
Tasks: 7 (limit: 4915)
Memory: 22.0M
CGroup: /system.slice/postgresql@14-main.service
├─2345 /usr/lib/postgresql/14/bin/postgres -D /var/lib/postgresql/14/main
└─2346 postgres: checkpointer process
Quick Reference
| Command | Description | Platform |
|---|---|---|
| sudo systemctl restart postgresql | Restart PostgreSQL service using systemd | Linux (systemd) |
| sudo service postgresql restart | Restart PostgreSQL service using SysV init | Older Linux |
| Restart via Services app | Restart PostgreSQL service from Windows Services | Windows |
| pg_ctl restart -D /path/to/data | Restart PostgreSQL manually using pg_ctl | Cross-platform |
Key Takeaways
Use system service commands like
systemctl restart postgresql to restart PostgreSQL safely.Always run restart commands with administrator or sudo privileges to avoid permission errors.
Check the PostgreSQL service status after restarting to confirm it is running.
On Windows, restart PostgreSQL via the Services app or with admin command prompt.
Ensure configuration changes are saved before restarting to apply them.