{"id":29934,"date":"2022-05-02T15:30:23","date_gmt":"2022-05-02T15:30:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=29934"},"modified":"2022-12-23T05:53:02","modified_gmt":"2022-12-23T05:53:02","slug":"postgresql-installation-and-configuration-tutorial-guide","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/postgresql-installation-and-configuration-tutorial-guide\/","title":{"rendered":"postgresql Installation and Configuration Tutorial Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">How To Install and Use PostgreSQL on Ubuntu?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1 \u2014 Installing PostgreSQL<\/h3>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">$ sudo apt update\n$ sudo apt install postgresql postgresql-contrib\n$ sudo systemctl start postgresql.service<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2 \u2014 Using PostgreSQL Roles and Databases<\/h3>\n\n\n\n<p>The installation procedure created a user account called <strong>postgres <\/strong>that is associated with the default <strong>Postgres <\/strong>role. In order to use Postgres, you can log into that account.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Switch over to the postgres account on your server by typing:\n\n$ sudo -i -u postgres\n\nYou can now access a Postgres prompt immediately by typing:\n$ psql\n\nThis will log you into the PostgreSQL prompt, and <span class=\"hljs-keyword\">from<\/span> here you are free to interact <span class=\"hljs-keyword\">with<\/span> the database management system right away.\n\nExit out <span class=\"hljs-keyword\">of<\/span> the PostgreSQL prompt by typing:\n&gt; \\q\nThis will bring you back to the postgres Linux command prompt.\n\nAccessing a Postgres Prompt Without Switching Accounts\n$ sudo -u postgres psql\n&gt; \\q<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3 \u2014 Creating a New Role<\/h3>\n\n\n\n<p>Currently, you just have the postgres role configured within the database. You can create new roles from the command line with the createrole command. The <strong>&#8211;interactive<\/strong> flag will prompt you for the name of the new role and also ask whether it should have superuser permissions.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">Currently, you just have the postgres role configured within the database. You can create <span class=\"hljs-keyword\">new<\/span> roles from the command line with the createrole command. \n\nThe --interactive flag will prompt you <span class=\"hljs-keyword\">for<\/span> the name of the <span class=\"hljs-keyword\">new<\/span> role <span class=\"hljs-keyword\">and<\/span> also ask whether it should have superuser permissions.\n\n<span class=\"hljs-keyword\">If<\/span> you are logged in <span class=\"hljs-keyword\">as<\/span> the &lt;strong&gt;postgres &lt;\/strong&gt;account, you can create a <span class=\"hljs-keyword\">new<\/span> user by typing:\n$ createuser --interactive\n\n<span class=\"hljs-keyword\">If<\/span>, instead, you prefer to <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">sudo<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">each<\/span> <span class=\"hljs-title\">command<\/span> <span class=\"hljs-title\">without<\/span> <span class=\"hljs-title\">switching<\/span> <span class=\"hljs-title\">from<\/span> <span class=\"hljs-title\">your<\/span> <span class=\"hljs-title\">normal<\/span> <span class=\"hljs-title\">account<\/span>, <span class=\"hljs-title\">type<\/span>:\n$ <span class=\"hljs-title\">sudo<\/span> -<span class=\"hljs-title\">u<\/span> <span class=\"hljs-title\">postgres<\/span> <span class=\"hljs-title\">createuser<\/span> --<span class=\"hljs-title\">interactive<\/span>\n<span class=\"hljs-title\">Output<\/span>\n<span class=\"hljs-title\">Enter<\/span> <span class=\"hljs-title\">name<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">role<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">add<\/span>: <span class=\"hljs-title\">sammy<\/span>\n<span class=\"hljs-title\">Shall<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">new<\/span> <span class=\"hljs-title\">role<\/span> <span class=\"hljs-title\">be<\/span> <span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">superuser<\/span>? (<span class=\"hljs-title\">y<\/span>\/<span class=\"hljs-title\">n<\/span>) <span class=\"hljs-title\">y<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"835\" height=\"387\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/05\/image-5.png\" alt=\"\" class=\"wp-image-29938\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/05\/image-5.png 835w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/05\/image-5-300x139.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/05\/image-5-768x356.png 768w\" sizes=\"auto, (max-width: 835px) 100vw, 835px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4 \u2014 Creating a New Database<\/h3>\n\n\n\n<p>Another assumption that the Postgres authentication system makes by default is that for any role used to log in, that <strong>role will have a database with the same name which it can access.<\/strong><\/p>\n\n\n\n<p><strong>This means that, if the user you created in the last section is called sammy, that role will attempt to connect to a database which is also called \u201csammy\u201d by default. <\/strong>You can create the appropriate database with the createdb command.<\/p>\n\n\n\n<p>If you are logged in as the postgres account, you would type something like:<br>$ createdb sammy.<\/p>\n\n\n\n<p>If, instead, you prefer to use sudo for each command without switching from your normal account, you would type:<br>$ sudo -u postgres createdb sammy<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5 \u2014 Opening a Postgres Prompt with the New Role<\/h3>\n\n\n\n<p>To log in with ident based authentication, you\u2019ll need a Linux user with the same name as your Postgres role and database.<\/p>\n\n\n\n<p>If you don\u2019t have a matching Linux user available, you can create one with the adduser command. You will have to do this from your non-root account with sudo privileges (meaning, not logged in as the postgres user):<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$ sudo adduser sammy\n\nOnce this <span class=\"hljs-keyword\">new<\/span> account is available, you can either <span class=\"hljs-keyword\">switch<\/span> over <span class=\"hljs-keyword\">and<\/span> connect to the database by typing:\n\n$ sudo -i -u sammy\npsql\n<span class=\"hljs-keyword\">Or<\/span>, you can <span class=\"hljs-keyword\">do<\/span> this inline:\n$ sudo -u sammy psql\n\n&lt;strong&gt;<span class=\"hljs-keyword\">If<\/span> you want your user to connect to a different database, you can <span class=\"hljs-keyword\">do<\/span> so by specifying the database like this:&lt;\/strong&gt;\n\npsql -d postgres\nOnce logged in, you can get check your current connection information by typing:\n\n\\conninfo\nOutput\nYou are connected to database <span class=\"hljs-string\">\"sammy\"<\/span> <span class=\"hljs-keyword\">as<\/span> user <span class=\"hljs-string\">\"sammy\"<\/span> via socket in <span class=\"hljs-string\">\"\/var\/run\/postgresql\"<\/span> at port <span class=\"hljs-string\">\"5432\"<\/span>.\nThis is useful <span class=\"hljs-keyword\">if<\/span> you are connecting to non-<span class=\"hljs-keyword\">default<\/span> databases <span class=\"hljs-keyword\">or<\/span> with non-<span class=\"hljs-keyword\">default<\/span> users.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>How To Install and Use PostgreSQL on Ubuntu? Step 1 \u2014 Installing PostgreSQL Step 2 \u2014 Using PostgreSQL Roles and Databases The installation procedure created a user account called postgres&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-29934","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=29934"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29934\/revisions"}],"predecessor-version":[{"id":29943,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/29934\/revisions\/29943"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=29934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=29934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=29934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}