{"id":16364,"date":"2021-05-14T13:35:40","date_gmt":"2021-05-14T11:35:40","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/"},"modified":"2021-05-14T13:35:40","modified_gmt":"2021-05-14T11:35:40","slug":"postgresql-pg_ctl-and-signals","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/","title":{"rendered":"PostgreSQL, pg_ctl and signals"},"content":{"rendered":"<p>I believe most people use <a href=\"https:\/\/www.postgresql.org\/docs\/current\/app-pg-ctl.html\" target=\"_blank\" rel=\"noopener\">pg_ctl<\/a> nowadays to start, stop, or restart a PostgreSQL instance. You can do all these tasks without using pg_ctl, but you&#8217;ll notice in a minute that pg_ctl really is your friend. Even if you&#8217;re using pg_ctl already, is is good to know what happens in the background and this is the scope of this post.<\/p>\n<p><!--more--><\/p>\n<p>For a moment, let&#8217;s imagine there is not pg_ctl: How can you start a PostgreSQL instance without it? The most simple solution to that is this:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pgdev] postgres -D \/u02\/pgdata\/DEV\/\n2021-05-14 15:00:47.309 CEST - 1 - 2793 -  - @ LOG:  redirecting log output to logging collector process\n2021-05-14 15:00:47.309 CEST - 2 - 2793 -  - @ HINT:  Future log output will appear in directory \"pg_log\".\n<\/pre>\n<p>The downside with this is, that PostgreSQL will run in the foreground and shutdown as soon as the session ends or you abort the command:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pgdev] CRTL-C\npostgres@debian10pg:\/home\/postgres\/ [pgdev] psql\npsql: error: connection to server on socket \"\/tmp\/.s.PGSQL.5432\" failed: No such file or directory\n        Is the server running locally and accepting connections on that socket?\n<\/pre>\n<p>Does this behavior make sense? It might not make sense when you start PostgreSQL manually, but it does make sense if:<\/p>\n<ul>\n<li>You want to start PostgreSQL with <a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/systemd.service.html\" target=\"_blank\" rel=\"noopener\">systemd<\/a> and you&#8217;re using Type=notify<\/li>\n<li>You run PostgreSQL in a container. In this case you do not want to detach, as the container is considered dead when the postmaster dies.<\/li>\n<\/ul>\n<p>There are probably other cases, but for me those are the two important ones.<\/p>\n<p>If you want to start PostgreSQL in the background you would need to do something like this:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pgdev] postgres -D \/u02\/pgdata\/DEV\/ &gt;\/var\/tmp\/pg.log 2&gt;&amp;1 &amp;\n[1] 2806\npostgres@debian10pg:\/home\/postgres\/ [pgdev] psql\npsql (14devel)\nType \"help\" for help.\n<\/pre>\n<p>Nothing is wrong with that, but how do you stop PostgreSQL without using pg_ctl? The answer are signals. Way back in 2012 I&#8217;ve written a small post about <a href=\"https:\/\/danielwestermann.com\/2012\/04\/17\/kill-is-not-really-about-killing\/\" target=\"_blank\" rel=\"noopener\">signals<\/a> and I think it is still worth reading. In very much the same way as written in this old post, you can control PostgreSQL by sending signals to the postmaster. For shutting down in &#8220;fast&#8221; mode, this is:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pgdev] head -1 \/u02\/pgdata\/DEV\/postmaster.pid \n2806\npostgres@debian10pg:\/home\/postgres\/ [pgdev] kill -SIGINT 2806\npostgres@debian10pg:\/home\/postgres\/ [pgdev] psql\npsql: error: connection to server on socket \"\/tmp\/.s.PGSQL.5432\" failed: No such file or directory\n        Is the server running locally and accepting connections on that socket?\n[1]+  Done                    postgres -D \/u02\/pgdata\/DEV\/ &gt; \/var\/tmp\/pg.log 2&gt;&amp;1\n<\/pre>\n<p>The other signals you can use for shutting down, are SIGTERM(smart) and SIGQUIT(immediate). pg_ctl is doing nothing different in the background, but of course it is much easier to use the switches of pg_ctl, than to remember the signals all the time. Here is a fragment of the source code from pg_ctl, where you can see the mapping of the signals to the shutdown modes:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\n        if (strcmp(modeopt, \"s\") == 0 || strcmp(modeopt, \"smart\") == 0)\n        {\n                shutdown_mode = SMART_MODE;\n                sig = SIGTERM;\n        }\n        else if (strcmp(modeopt, \"f\") == 0 || strcmp(modeopt, \"fast\") == 0)\n        {\n                shutdown_mode = FAST_MODE;\n                sig = SIGINT;\n        }\n        else if (strcmp(modeopt, \"i\") == 0 || strcmp(modeopt, \"immediate\") == 0)\n        {\n                shutdown_mode = IMMEDIATE_MODE;\n                sig = SIGQUIT;\n        }\n<\/pre>\n<p>The same is true for starting up: pg_ctl is starting PostgreSQL in the background automatically and you do not need to care about this:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pgdev] pg_ctl start\nwaiting for server to start....2021-05-14 15:18:07.159 CEST - 1 - 2856 -  - @ LOG:  redirecting log output to logging collector process\n2021-05-14 15:18:07.159 CEST - 2 - 2856 -  - @ HINT:  Future log output will appear in directory \"pg_log\".\n done\nserver started\n<\/pre>\n<p>There is another signal you can use: SIGHUP. When you change a configuration in postgresql.conf, postgresql.auto.conf or pg_hba.conf you need to tell the postmaster about that change. Given the parameter can be changed online, or a change in pg_hba.conf (which can always be done online) you can tell the postmaster to re-read it&#8217;s configuration files:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pgdev] head -1 \/u02\/pgdata\/DEV\/postmaster.pid \n2856\npostgres@debian10pg:\/home\/postgres\/ [pgdev] kill -SIGHUP 2856\n<\/pre>\n<p>Take a look at the log file and you&#8217;ll see the exact same messages, as if you would have used &#8220;pg_ctl reload&#8221;:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\n2021-05-14 15:21:02.623 CEST - 8 - 2856 -  - @ LOG:  received SIGHUP, reloading configuration files\n<\/pre>\n<p>Instead of using <a href=\"https:\/\/www.postgresql.org\/docs\/current\/functions-admin.html\" target=\"_blank\" rel=\"noopener\">pg_terminate_backend<\/a> to terminate a process, you can send a signal instead:<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1\">\npostgres@debian10pg:\/home\/postgres\/ [pg14] ps -ef | grep psql\npostgres  2908  2289  0 15:30 pts\/1    00:00:00 psql\npostgres  3086  2918  0 15:31 pts\/0    00:00:00 grep psql\npostgres@debian10pg:\/home\/postgres\/ [pg14] kill -SIGTERM 2908\n-- the session of the backend reports:\npsql (14devel)\nType \"help\" for help.\n\n[local]:5432 postgres@postgres=# Terminated\n<\/pre>\n<p>You see, pg_ctl and pg_terminate_backend are your friends, as it is more clear what happens if you can use something descriptive, instead of sending signals directly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I believe most people use pg_ctl nowadays to start, stop, or restart a PostgreSQL instance. You can do all these tasks without using pg_ctl, but you&#8217;ll notice in a minute that pg_ctl really is your friend. Even if you&#8217;re using pg_ctl already, is is good to know what happens in the background and this is [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229],"tags":[77],"type_dbi":[],"class_list":["post-16364","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","tag-postgresql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>PostgreSQL, pg_ctl and signals - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL, pg_ctl and signals\" \/>\n<meta property=\"og:description\" content=\"I believe most people use pg_ctl nowadays to start, stop, or restart a PostgreSQL instance. You can do all these tasks without using pg_ctl, but you&#8217;ll notice in a minute that pg_ctl really is your friend. Even if you&#8217;re using pg_ctl already, is is good to know what happens in the background and this is [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-14T11:35:40+00:00\" \/>\n<meta name=\"author\" content=\"Open source Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Open source Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/\"},\"author\":{\"name\":\"Open source Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"headline\":\"PostgreSQL, pg_ctl and signals\",\"datePublished\":\"2021-05-14T11:35:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/\"},\"wordCount\":509,\"commentCount\":0,\"keywords\":[\"PostgreSQL\"],\"articleSection\":[\"Database Administration &amp; Monitoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/\",\"name\":\"PostgreSQL, pg_ctl and signals - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2021-05-14T11:35:40+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/postgresql-pg_ctl-and-signals\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL, pg_ctl and signals\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/4bae5e5dd8c7d48edbd948772b49b202\",\"name\":\"Open source Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g\",\"caption\":\"Open source Team\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/open-source-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"PostgreSQL, pg_ctl and signals - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL, pg_ctl and signals","og_description":"I believe most people use pg_ctl nowadays to start, stop, or restart a PostgreSQL instance. You can do all these tasks without using pg_ctl, but you&#8217;ll notice in a minute that pg_ctl really is your friend. Even if you&#8217;re using pg_ctl already, is is good to know what happens in the background and this is [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/","og_site_name":"dbi Blog","article_published_time":"2021-05-14T11:35:40+00:00","author":"Open source Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Open source Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/"},"author":{"name":"Open source Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"headline":"PostgreSQL, pg_ctl and signals","datePublished":"2021-05-14T11:35:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/"},"wordCount":509,"commentCount":0,"keywords":["PostgreSQL"],"articleSection":["Database Administration &amp; Monitoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/","url":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/","name":"PostgreSQL, pg_ctl and signals - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2021-05-14T11:35:40+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/postgresql-pg_ctl-and-signals\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL, pg_ctl and signals"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/4bae5e5dd8c7d48edbd948772b49b202","name":"Open source Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/eb4fb12e386e8c41fdef0733e8114594cf2653e4f55e9fa2161442b8eaf3f657?s=96&d=mm&r=g","caption":"Open source Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/open-source-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/16364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=16364"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/16364\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=16364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=16364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=16364"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=16364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}