{"id":628,"date":"2023-10-01T13:07:34","date_gmt":"2023-10-01T10:07:34","guid":{"rendered":"http:\/\/pgdataeraold.local\/?p=628"},"modified":"2023-10-01T13:07:34","modified_gmt":"2023-10-01T10:07:34","slug":"starting-and-stopping-postgresql-instance","status":"publish","type":"post","link":"https:\/\/pgdataera.com\/en\/starting-and-stopping-postgresql-instance\/","title":{"rendered":"Starting and Stopping Postgresql Instance"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" style=\"font-size:24px\">Starting Postgresql Instance<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"717\" height=\"172\" src=\"http:\/\/pgdataeraold.local\/wp-content\/uploads\/2023\/09\/image-22.png\" alt=\"\" class=\"wp-image-603\" srcset=\"https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/09\/image-22.png 717w, https:\/\/pgdataera.com\/wp-content\/uploads\/2023\/09\/image-22-300x72.png 300w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>If installed from pre-built packages (yum\/apt install postgresql\u2026), services are<br>created according to the operating system. Thus, it can be stopped and started with<br>systemctl commands and its startup can be automated (systemctl enable<br>postgres).<\/p>\n\n\n\n<p>Note: In the appendix chapters, where the installation and architecture from the prebuilt<br>package were explained, and in the first chapter, you can examine the installation<br>example from the source code. (to access the installation from the source kod <strong><mark style=\"color:#088395\" class=\"has-inline-color\"><a href=\"http:\/\/pgdataeraold.local\/category\/postgresql-database-architecture\/storage-structure\/\" data-type=\"link\" data-id=\"http:\/\/pgdataeraold.local\/category\/postgresql-database-architecture\/storage-structure\/\">&#8211;&gt;<\/a> <\/mark><\/strong>)<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>Cluster (data directory)<\/td><td>\/pg\/data\/13.6<\/td><\/tr><tr><td>Binary location<\/td><td>\/pg\/home\/13.6\/bin<\/td><\/tr><tr><td>Port (changed due to other instance)<\/td><td>5434<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">The database to be used in the examples is built from the source code. Directory information is as table.<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"postgresql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># The following lines can be added to the bottom of the .profile\nfile in order repeat the full path every time.\n\nvim \/var\/lib\/postgresql\/.profile\nexport PGDATA=\/pg\/data\/13.6\nexport PGDATABASE=postgres\nexport PGUSER=postgres\nexport PGPORT=5434\nexport PATH=\/pg\/home\/13.6\/bin:$PATH\n\n# In order to enable the current variables in the current session.\nsource ~\/.profile\n\n# To check.\npostgres@srv1:~$ which postgres\n\/pg\/home\/13.6\/bin\/postgres<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>The following commands are executed to start the database.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"postgresql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">postgres@srv1:~$ postgres -D \/pg\/data\/13.6\/\nLOG:starting PostgreSQL 13.6 on x86_64-pc-linux-gnu,compiled by\ngcc (Debian 8.3.0-6)8.3.0,64-bit\n[9860] LOG: listening on IPv4 address \u201c0.0.0.0\u201d, port 5434\n[9860] LOG: listening on IPv6 address \u201c::\u201d, port 5434\n[9860] LOG: listening on Unix socket \u201c\/tmp\/.s.PGSQL.5434\u201d\n[9861] LOG: database system was shut down at 16:13:34 +03\n[9860] LOG: database system is ready to accept connections<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>Note: In this way, the postgresql instance runs in the foreground. If the SSH session is<br>terminated, the database is also closed.<br>If PGDATA is defined in environment variables, \u201c\u2013D\u201d parameter is not required.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"postgresql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">In this manner, stdout and sterr will be directed to the log\nfile by running in the background.\n\npostgres -D \/pg\/data\/13.6\/ &gt; postgresql.log 2&gt;&amp;1 &amp;\n\n# To run in debug mode (-d 5), should be used with caution as\nlog file grows very quickly.\n\npostgres -D \/pg\/data\/13.6\/ -d 5 &gt; debugpostgres.log 2&gt;&amp;1 &amp;<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p><code><strong>postgres :<\/strong><\/code> It can take many parameters, there are details on man postgres or postgresql.org.<\/p>\n\n\n\n<p><strong><code>pg_ctl :<\/code><\/strong> It works similarly to postgres described above, but <code>pg_ctl <\/code>is more useful.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Runs in the background and redirects the log to the\npostgresql.log file.\n\npostgres@srv1:~$ pg_ctl start -l postgresql.log\n\n# -D option can be used here as well.\n\npg_ctl -D \/pg\/data\/13.6\/ -l postgresql.log 2&gt;&amp;1 &amp;\n\n# Status, stop and restart options are also available.\n\npostgres@srv1:~$ pg_ctl status\/stop\/restart -l postgresql.log\n\n# Reload reads the configuration file and enables the settings\nthat do not require a restart.\n\npostgres@srv1:~$ pg_ctl reload -D \/pg\/data\/13.6\n\n# \u2018promote\u2019 option switches the hot standby server to primary,\nthat is, it opens it for writing and reading.\n\npostgres@srv1:~$ pg_ctl promote -D \/pg\/data\/13.6<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># With root privilege, systemctl can be started and stopped.\n\nsudo systemctl start\/stop\/restart postgresql@14-main.service\n\n# To check status\n\nsudo systemctl status postgresql@14-main.service\n\n# With enable option, it automatically starts and stops\npostgres on startup and shutdown.\n\nsudo systemctl enable postgresql@14-main.service<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p><code><strong>systemctl :<\/strong><\/code>Startup and shutdown scripts can be integrated with the operating system\u2019s<br>systemd service management tool. The database can be automatically stopped and<br>started when the operating system is shut down or booted. Systemd services come<br>automatically when installed from in-build packages.<\/p>\n\n\n\n<p><code><strong>pg_ctlcluster : <\/strong><\/code>Simplifies systemd integration. In multi-cluster environments, it calls the<br>cluster directory of the database to be processed and the correct <code>pg_ctl.<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"font-size:24px\"> Checking Instance<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pgrep -u postgres -fa -- -D\n\n5932 \/pg\/home\/13.6\/bin\/postgres -D \/pg\/data\/13.6 --config-file=\/pg\/data\/13.6\/postgresql.conf\n\npgrep -fa -- -D |grep postgres\n\n5932 \/pg\/home\/13.6\/bin\/postgres -D \/pg\/data\/13.6 \u2013config file=\/pg\/data\/13.6\/postgresql.conf\n\n# Port information\n\nsed -n 4p $PGDATA\/postmaster.pid\n5434\n# or\nsed<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p>With pgrep and sed tools, it is possible to get information such as the process<br>number (PID) and TCP port of the config file where the instance is running.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"font-size:24px\">Stopping Postgresql Instance<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"postgresql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">postgres@srv1:~$ pg_ctl -D \/pg\/data\/13.6 stop -m s\n\nwaiting for server to shut down....................... done\n\npostgres@srv1:~$ pg_ctl -D \/pg\/data\/13.6 stop -m f\n\nwaiting for server to shut down.... done\nserver stopped\n\npostgres@srv1:~$ pg_ctl -D \/pg\/data\/13.6 stop -m i\n\nwaiting for server to shut down.... done\nserver stopped<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-vivid-green-cyan-color has-alpha-channel-opacity has-vivid-green-cyan-background-color has-background is-style-wide\" \/>\n\n\n\n<p><strong>Smart Mode:<\/strong> (SIGTERM) New sessions (connections) are not allowed. It doesn\u2019t<br>terminate existing sessions, it will let it run. However, it stops the instance when all<br>sessions are terminated. In a production environment database, you may have to wait<br>long time (maybe months) for all sessions to expire.<\/p>\n\n\n\n<p><strong>Fast Mode: <\/strong>(SIGINT) New sessions (connections) are not allowed. Terminates current<br>sessions. if it is committed, all sessions are terminated and if the user does not commit,<br>it rollbacks. Stops fast and consistent. No recovery is required at startup. It is the<br>default shutdown mode.<\/p>\n\n\n\n<p><strong>Immediate Mode:<\/strong> Immediately terminates all server processes. It\u2019s the same as<br>unplugging the server\u2019s power.<br>The changed buffer area is closed inconsistently because the datafiles cannot be<br>written and the uncommitted changes cannot be rolled back.<br>Requires recovery from transaction (WAL \u2013 write-ahead-log) log files at startup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Starting Postgresql Instance If installed from pre-built packages (yum\/apt install postgresql\u2026), services arecreated according to the operating system. Thus, it can be stopped and started withsystemctl commands and its startup can be automated (systemctl enablepostgres). Note: In the appendix chapters, where the installation and architecture from the prebuiltpackage were explained, and in the first chapter, <a href=\"https:\/\/pgdataera.com\/en\/starting-and-stopping-postgresql-instance\/\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  Starting and Stopping Postgresql Instance<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[284],"tags":[],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-instance-network-connection-management"],"_links":{"self":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":0,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"wp:attachment":[{"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pgdataera.com\/en\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}