pg_ctl start/stop vs systemctl start/stop postgresql.service

pg_ctl is a postgresql command that created with potgresql software installation. you can start,stop and many other operations with pg_ctl. If you start postgresql with pg_ctl then postgresql cluster start normally and can run with no problems. with starting with pg_ctl you decided a manual start/stop. if you have operatig system service as postgesql-13.service the OS service do not informed about service start or stop. Because postgresql OS service executes /usr/lib/systemd/system/postgresql-13.service script actually and in this script there is not any control steps. Service script just control the data directories and then try to start a postmater process with controlled data directory. Therefore if postgresql was started with pg_ctl, trying OS “service start” fails. Below I will try to representive the stuation.

consequently, it is advised to use OS service. If you use OS service you can easly start and stop postgresql cluster after a reboot, without any intervention.

–psotgresql and OS service is down

	[root@postgresstb data]# systemctl status postgresql-13.service 
	● postgresql-13.service - PostgreSQL 13 database server
	   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
	   Active: inactive (dead)
		 Docs: https://www.postgresql.org/docs/13/static/
		 
	[root@postgresstb data]# ps -ef | grep -i postmaster | grep -v grep
	[root@postgresstb data]#

–start the postgresql cluster with pg_ctl

		-bash-4.2$ /usr/pgsql-13/bin/pg_ctl start
		waiting for server to start....2021-04-01 15:37:16.847 +03 [11628] LOG:  redirecting log output to logging collector process
		2021-04-01 15:37:16.847 +03 [11628] HINT:  Future log output will appear in directory "log".
		 done
		server started
		-bash-4.2$ psql
		psql (13.2)
		Type "help" for help.

		postgres=# \q


		postgres 11670 11554  0 15:37 pts/0    00:00:00 grep --color=auto -i postmaster
		-bash-4.2$ ps -ef | grep -i postgres
		root     11552  1827  0 15:37 pts/0    00:00:00 su - postgres
		postgres 11554 11552  0 15:37 pts/0    00:00:00 -bash
		postgres 11628     1  0 15:37 ?        00:00:00 /usr/pgsql-13/bin/postgres
		postgres 11629 11628  0 15:37 ?        00:00:00 postgres: logger 
		postgres 11631 11628  0 15:37 ?        00:00:00 postgres: checkpointer 
		postgres 11632 11628  0 15:37 ?        00:00:00 postgres: background writer
		postgres 11633 11628  0 15:37 ?        00:00:00 postgres: walwriter 
		postgres 11634 11628  0 15:37 ?        00:00:00 postgres: autovacuum launcher
		postgres 11635 11628  0 15:37 ?        00:00:00 postgres: stats collector 
		postgres 11636 11628  0 15:37 ?        00:00:00 postgres: logical replication launcher
		postgres 11682 11554  0 15:37 pts/0    00:00:00 ps -ef
		postgres 11683 11554  0 15:37 pts/0    00:00:00 grep --color=auto -i postgres
		-bash-4.2$

–check the OS service

		[root@postgresstb data]# systemctl status postgresql-13.service
		● postgresql-13.service - PostgreSQL 13 database server
		   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
		   Active: inactive (dead)
			 Docs: https://www.postgresql.org/docs/13/static/

		Apr 01 14:29:06 postgresstb postmaster[7485]: 2021-04-01 14:29:06.935 +03 ....
		Apr 01 14:29:06 postgresstb systemd[1]: Started PostgreSQL 13 database server.
		Apr 01 14:29:18 postgresstb systemd[1]: Stopping PostgreSQL 13 database se....
		Apr 01 14:29:18 postgresstb systemd[1]: Stopped PostgreSQL 13 database server.
		Apr 01 14:29:55 postgresstb systemd[1]: Starting PostgreSQL 13 database se....
		Apr 01 14:29:56 postgresstb postmaster[7554]: 2021-04-01 14:29:56.033 +03 ...s
		Apr 01 14:29:56 postgresstb postmaster[7554]: 2021-04-01 14:29:56.033 +03 ....
		Apr 01 14:29:56 postgresstb systemd[1]: Started PostgreSQL 13 database server.
		Apr 01 15:35:52 postgresstb systemd[1]: Stopping PostgreSQL 13 database se....
		Apr 01 15:35:52 postgresstb systemd[1]: Stopped PostgreSQL 13 database server.
		Hint: Some lines were ellipsized, use -l to show in full.
		

–start the postgresql OS service

		[root@postgresstb data]# systemctl start  postgresql-13.service
		Job for postgresql-13.service failed because the control process exited with error code. See "systemctl status postgresql-13.service" and "journalctl -xe" for details.
		[root@postgresstb data]# systemctl status postgresql-13.service
		● postgresql-13.service - PostgreSQL 13 database server
		   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
		   Active: failed (Result: exit-code) since Thu 2021-04-01 15:40:32 +03; 4s ago
			 Docs: https://www.postgresql.org/docs/13/static/
		  Process: 11853 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
		  Process: 11847 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
		 Main PID: 11853 (code=exited, status=1/FAILURE)

		Apr 01 15:40:32 postgresstb systemd[1]: Starting PostgreSQL 13 database se....
		Apr 01 15:40:32 postgresstb systemd[1]: postgresql-13.service: main proces...E
		Apr 01 15:40:32 postgresstb systemd[1]: Failed to start PostgreSQL 13 data....
		Apr 01 15:40:32 postgresstb systemd[1]: Unit postgresql-13.service entered....
		Apr 01 15:40:32 postgresstb systemd[1]: postgresql-13.service failed.
		Hint: Some lines were ellipsized, use -l to show in full.

–psotgresql OS service failed, but psotgresql cluster runnig. Now lets try to stop and restart postgaresql OS service.

		[root@postgresstb data]# systemctl stop postgresql-13.service  
		[root@postgresstb data]# systemctl status postgresql-13.service
		● postgresql-13.service - PostgreSQL 13 database server
		   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
		   Active: failed (Result: exit-code) since Thu 2021-04-01 15:40:32 +03; 2min 30s ago
			 Docs: https://www.postgresql.org/docs/13/static/
		  Process: 11853 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
		  Process: 11847 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
		 Main PID: 11853 (code=exited, status=1/FAILURE)

		Apr 01 15:40:32 postgresstb systemd[1]: Starting PostgreSQL 13 database server...
		Apr 01 15:40:32 postgresstb systemd[1]: postgresql-13.service: main process exited, code=exited, status=1/FAILURE
		Apr 01 15:40:32 postgresstb systemd[1]: Failed to start PostgreSQL 13 database server.
		Apr 01 15:40:32 postgresstb systemd[1]: Unit postgresql-13.service entered failed state.
		Apr 01 15:40:32 postgresstb systemd[1]: postgresql-13.service failed.
		[root@postgresstb data]# 
		[root@postgresstb data]# 
		[root@postgresstb data]# 
		[root@postgresstb data]# systemctl restart postgresql-13.service
		Job for postgresql-13.service failed because the control process exited with error code. See "systemctl status postgresql-13.service" and "journalctl -xe" for details.
		[root@postgresstb data]# 
		[root@postgresstb data]# 
		[root@postgresstb data]# systemctl status postgresql-13.service 
		● postgresql-13.service - PostgreSQL 13 database server
		   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
		   Active: failed (Result: exit-code) since Thu 2021-04-01 15:43:14 +03; 3s ago
			 Docs: https://www.postgresql.org/docs/13/static/
		  Process: 12015 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
		  Process: 12009 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
		 Main PID: 12015 (code=exited, status=1/FAILURE)

		Apr 01 15:43:14 postgresstb systemd[1]: Starting PostgreSQL 13 database server...
		Apr 01 15:43:14 postgresstb systemd[1]: postgresql-13.service: main process exited, code=exited, status=1/FAILURE
		Apr 01 15:43:14 postgresstb systemd[1]: Failed to start PostgreSQL 13 database server.
		Apr 01 15:43:14 postgresstb systemd[1]: Unit postgresql-13.service entered failed state.
		Apr 01 15:43:14 postgresstb systemd[1]: postgresql-13.service failed.
		[root@postgresstb data]# 
		[root@postgresstb data]# 
[root@postgresstb data]# 

–to resolve this problem we need to stop postgresql with pg_ctl and then start postgresql cluster with OS service.

		[root@postgresstb data]# su - postgres
		Last login: Thu Apr  1 15:37:08 +03 2021 on pts/0
		-bash-4.2$ /usr/pgsql-13/bin/pg_ctl stop           
		waiting for server to shut down.... done
		server stopped
		-bash-4.2$ psql
		psql: error: could not connect to server: No such file or directory
				Is the server running locally and accepting
				connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
		-bash-4.2$ logout
		[root@postgresstb data]# systemctl status postgresql-13.service     
		● postgresql-13.service - PostgreSQL 13 database server
		   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
		   Active: failed (Result: exit-code) since Thu 2021-04-01 15:44:42 +03; 49s ago
			 Docs: https://www.postgresql.org/docs/13/static/
		  Process: 12122 ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} (code=exited, status=1/FAILURE)
		  Process: 12115 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
		 Main PID: 12122 (code=exited, status=1/FAILURE)

		Apr 01 15:44:42 postgresstb systemd[1]: Starting PostgreSQL 13 database server...
		Apr 01 15:44:42 postgresstb systemd[1]: postgresql-13.service: main process exited, code=exited, status=1/FAILURE
		Apr 01 15:44:42 postgresstb systemd[1]: Failed to start PostgreSQL 13 database server.
		Apr 01 15:44:42 postgresstb systemd[1]: Unit postgresql-13.service entered failed state.
		Apr 01 15:44:42 postgresstb systemd[1]: postgresql-13.service failed.
		[root@postgresstb data]# 
		[root@postgresstb data]# 
		[root@postgresstb data]# systemctl start  postgresql-13.service
		[root@postgresstb data]# 
		[root@postgresstb data]# systemctl status postgresql-13.service
		● postgresql-13.service - PostgreSQL 13 database server
		   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
		   Active: active (running) since Thu 2021-04-01 15:45:43 +03; 1s ago
			 Docs: https://www.postgresql.org/docs/13/static/
		  Process: 12274 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
		 Main PID: 12280 (postmaster)
		   CGroup: /system.slice/postgresql-13.service
				   ├─12280 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
				   ├─12282 postgres: logger 
				   ├─12284 postgres: checkpointer 
				   ├─12285 postgres: background writer 
				   ├─12286 postgres: walwriter 
				   ├─12287 postgres: autovacuum launcher 
				   ├─12288 postgres: stats collector 
				   └─12289 postgres: logical replication launcher 

		Apr 01 15:45:43 postgresstb systemd[1]: Starting PostgreSQL 13 database server...
		Apr 01 15:45:43 postgresstb postmaster[12280]: 2021-04-01 15:45:43.133 +03 [12280] LOG:  redirecting log output to logging collector process
		Apr 01 15:45:43 postgresstb postmaster[12280]: 2021-04-01 15:45:43.133 +03 [12280] HINT:  Future log output will appear in directory "log".
		Apr 01 15:45:43 postgresstb systemd[1]: Started PostgreSQL 13 database server.
		[root@postgresstb data]# 
		[root@postgresstb data]# su - postgres
		Last login: Thu Apr  1 15:45:10 +03 2021 on pts/0
		-bash-4.2$ psql
		psql (13.2)
		Type "help" for help.

		postgres=# \q
		-bash-4.2$ ps -ef | grep -i postgres    
		postgres 12280     1  0 15:45 ?        00:00:00 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
		postgres 12282 12280  0 15:45 ?        00:00:00 postgres: logger 
		postgres 12284 12280  0 15:45 ?        00:00:00 postgres: checkpointer 
		postgres 12285 12280  0 15:45 ?        00:00:00 postgres: background writer 
		postgres 12286 12280  0 15:45 ?        00:00:00 postgres: walwriter 
		postgres 12287 12280  0 15:45 ?        00:00:00 postgres: autovacuum launcher 
		postgres 12288 12280  0 15:45 ?        00:00:00 postgres: stats collector 
		postgres 12289 12280  0 15:45 ?        00:00:00 postgres: logical replication launcher 
		root     12298  1827  0 15:45 pts/0    00:00:00 su - postgres
		postgres 12299 12298  1 15:45 pts/0    00:00:00 -bash
		postgres 12366 12299  0 15:45 pts/0    00:00:00 ps -ef
		postgres 12367 12299  0 15:45 pts/0    00:00:00 grep --color=auto -i postgres

you can use pg_ctl still, but when you use pg_ctl OS service fails and the stuations above will accour again.

Leave a comment