PostgreSQL Services - postgresql

Can someone give a detailed explanation of how the postgresql service(s) works on Ubuntu [16.04]? The behavior that leads me to ask is the following. I use the command sudo systemctl start postgresql to start my postgresql service. When I run systemctl list-units | grep post I see
UNIT LOAD ACTIVE SUB DESCRIPTION
postgresql.service loaded active exited PostgreSQL RDBMS
postgresql#9.6-main.service loaded active running PostgreSQL Cluster 9.6-main
system-postgresql.slice loaded active active system-postgresql.slice
and when I run sudo systemctl stop postgresql both postgresql.service and postgresql#9.6-main.service disappear. What is each of these three services doing?

I assume postgresql#9.6-main.service is registered service for PostgreSQL Cluster 9.6-main cluster. This is the one that actually controls postgres on your system.
I assume postgresql.service is the effect desribed here
I assume system-postgresql.slice is a postgres slice unit
none of above are critical for running postgres. You can start/stop the cluser with just pg_ctl, without registering it with your services. But if you wonder which service runs it in your case - then it is postgresql#9.6-main.service

Related

Ubuntu - Failed to stop postgres.service: Unit postgres.service not loaded

I tried to stop postgres with the command systemctl stop postgres and I got this error. Now I can't start postgres in a docker container because the port is already used.
I fixed, I had to use the command systemctl stop postgresql instead of systemctl stop postgres.

System has not been booted with systemd ERROR when updating Postgresql in Ubuntu

I've been following this link to update Postgresql on my Ubuntu-18.04 (VM running on Windows 10)
https://computingforgeeks.com/install-postgresql-12-on-ubuntu/
However, Im getting the below error, so I cannot proceed.
.../desktop/my_app$ systemctl status postgresql.service
System has not been booted with systemd as init system (PID 1). Can't operate.
What can I do?
service postgresql status
or
systemctl status postgresql
Must work.

Failed to restart mongod.service : Unit mongod.service not found

There are a lot of variations for this question, on different forums. I tried a lot of things to get it to work. I am using AWS EC2 and MEAN by Bitnami, I tried connecting using Node JS and I realized that my monogodb service is not running. I checked it by running on the terminal (connected using Putty)
service mongod status
This is the error I get
mongodb.service Loaded:not-found (Reason: No such file or directory)
Active: inactive(dead)
To try my luck, I tried
sudo service mongod restart
And I get this error:
Failed to restart mongod.service : Unit mongod.service not found
Now, just to probe more I tried looking if I have this service installed.
I ran this command: ls /lib/systemd/system
And it gave a huge list, but I couldn't find mongod.service anywhere.
My Ubuntu Ver: 16.04
I am guessing it's not present or maybe I am looking for the wrong stuff. Please let me know how do I get the service to run. I am sort of new to MongoDB and Bitnami.
Each Bitnami MEAN stack includes a control script that lets you easily stop, start and restart services.
The script is located at /opt/bitnami/ctlscript.sh.
To start all services:
sudo /opt/bitnami/ctlscript.sh start
To start a single service:
sudo /opt/bitnami/ctlscript.sh start <service name>
So to answer your question:
sudo /opt/bitnami/ctlscript.sh start mongod
You can obtain a list of available services and operations by running the script without any arguments:
sudo /opt/bitnami/ctlscript.sh

Weird systemctl behavior

I installed Postgres9.6 on my Ubuntu 16.04 through apt-get. and I installed the same version on an AWS ubuntu instance to try replication.
I was following the steps from this blog:
http://www.rassoc.com/gregr/weblog/2013/02/16/zero-to-postgresql-streaming-replication-in-10-mins/
Now I can log to the postgres user on both machines, and run psql and execute all database operations. But when I check the status of the server (on both machines) using sudo systemctl status postgres.service, I get this:
● postgres.service Loaded: not-found (Reason: No such file or
directory) Active: inactive (dead)
Which is weird because I can run psql, right?
And this prevents me from creating remote connections.

How to restart Postgresql

I have PostgreSQL 9.3 and 9.4 installed on my Linux Mint machine.
How can I restart PostgreSQL 9.4?
A method to restart both versions together is also fine.
Try this as root (maybe you can use sudo or su):
/etc/init.d/postgresql restart
Without any argument the script also gives you a hint on how to restart a specific version
[Uqbar#Feynman ~] /etc/init.d/postgresql
Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status} [version ...]
Similarly, in case you have it, you can also use the service tool:
[Uqbar#Feynman ~] service postgresql
Usage: /etc/init.d/postgresql {start|stop|restart|reload|force reload|status} [version ...]
Please, pay attention to the optional [version ...] trailing argument.
That's meant to allow you, the user, to act on a specific version, in case you were running multiple ones. So you can restart version X while keeping version Y and Z untouched and running.
Finally, in case you are running systemd, then you can use systemctl like this:
[Uqbar#Feynman ~] systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2017-11-14 12:33:35 CET; 7min ago
...
You can replace status with stop, start or restart as well as other actions. Please refer to the documentation for full details.
In order to operate on multiple concurrent versions, the syntax is slightly different. For example to stop v12 and reload v13 you can run:
systemctl stop postgresql-12.service
systemctl reload postgresql-13.service
Thanks to #Jojo for pointing me to this very one.
Finally Keep in mind that root permissions may be needed for non-informative tasks as in the other cases seen earlier.
You can also restart postgresql by using this command, should work on both the versions:
sudo service postgresql restart
On Windows :
1-Open Run Window by Winkey + R
2-Type services.msc
3-Search Postgres service based on version installed.
4-Click stop, start or restart the service option.
On Linux :
sudo systemctl restart postgresql
also instead of "restart" you can replace : stop or status.
This should work:
sudo systemctl stop postgresql
sudo systemctl start postgresql
The right way to restart postgres server is to use
pg_ctl restart -D /home/postgres/postgresdbdata/
pg_ctl is usually found in location usr/local/pgsql/bin/pg_ctl if u dont have it added to your path.
Documentation can be found here
https://www.postgresql.org/docs/13/app-pg-ctl.html
This has more details on Start,Stop,Restart,etc of the postgres server.
You can firstly check for the postgress process running
ps -ef | grep post
You might need to use (post|pg) on the grep side, to discover the process for the Postgres service running on your machine, it might depend on your OS.
And you can just kill the child process since most of this process should restart automatically.
sudo kill $pid_of_the_child_process
macOS:
On the top left of the MacOS menu bar you have the Postgres Icon
Click on it this opens a drop down menu
Click on Stop -> than click on start