{"id":1681,"date":"2024-08-25T08:56:55","date_gmt":"2024-08-25T08:56:55","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1681"},"modified":"2025-05-23T17:58:33","modified_gmt":"2025-05-23T17:58:33","slug":"postgresql-basic-commands","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1681","title":{"rendered":"PostgreSQL: Basic commands"},"content":{"rendered":"<p>We will use a CentOS server to install PostgreSQL. The below command installs the PostgreSQL server.<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> dnf install postgresql15-server -y<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> postgresql-setup initdb<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> systemctl start postgresql<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> systemctl status postgresql<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> systemctl enable postgresql<\/p>\n<p>The PostgreSQL listens on the localhost by default. If other servers such as application servers connect remotely to this DB server, we need to modify the below configuration file.<\/p>\n<p>Add the below line at the end of the file &#8220;<em>\/var\/lib\/pgsql\/data\/pg_hba.conf<\/em>&#8221; after keeping a copy of the original file elsewhere:<\/p>\n<p><em>host all all 0.0.0.0\/0 md5<\/em><\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> systemctl restart postgresql<\/p>\n<p>Now a new user postgres will be automatically created<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> cat \/etc\/passwd | grep postgres<br \/>\npostgres:x:26:26:PostgreSQL Server:\/var\/lib\/pgsql:\/bin\/bash<\/p>\n<p>Now, let us access the newly created BD service and get to the\u00a0<em>postgreSQL<\/em> prompt:<\/p>\n<p><strong>[root@SHIJU-TEST-PGSQL-01 ~]#<\/strong> su &#8211; postgres<br \/>\nLast login: Sun Aug 25 09:24:00 UTC 2024 on pts\/3<\/p>\n<p><strong>[<span style=\"color: #993300;\">postgres<\/span>@SHIJU-TEST-PGSQL-01 ~]$<\/strong> psql<\/p>\n<p><em>====== ===<\/em><br \/>\n<em>psql (15.8)<\/em><br \/>\n<em>Type &#8220;help&#8221; for help.<\/em><br \/>\n<span style=\"color: #993300;\"><em>postgres=#<\/em><\/span><br \/>\n<em>====== ===<\/em><\/p>\n<p>The command to list all existing databases in the server is &#8220;<strong>\\l<\/strong>&#8221; as follows:<\/p>\n<p><strong>postgres=#<\/strong> \\l<br \/>\n<em>====== ====<\/em><br \/>\n<em> List of databases<\/em><br \/>\n<em> Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges<\/em><br \/>\n<em>&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/em><br \/>\n<em> postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc |<\/em><br \/>\n<em> template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc | =c\/postgres +<\/em><br \/>\n<em> | | | | | | | postgres=CTc\/postgres<\/em><br \/>\n<em> template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc | =c\/postgres +<\/em><br \/>\n<em> | | | | | | | postgres=CTc\/postgres<\/em><br \/>\n<em>(3 rows)<\/em><\/p>\n<p><em>postgres=#<\/em><br \/>\n<em>====== ====<\/em><\/p>\n<p>Now let us assign\/reset DB root user password:<\/p>\n<p><strong>postgres=#<\/strong> \\password<br \/>\n<em>===== ===<\/em><br \/>\n<em> Enter new password for user &#8220;postgres&#8221;: **********<\/em><br \/>\n<em> Enter it again:\u00a0**********<\/em><br \/>\n<em> postgres=#<\/em><br \/>\n<em>===== ===<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>We will use a CentOS server to install PostgreSQL. The below command installs the PostgreSQL server. [root@SHIJU-TEST-PGSQL-01 ~]# dnf install postgresql15-server -y [root@SHIJU-TEST-PGSQL-01 ~]# postgresql-setup <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1681\" title=\"PostgreSQL: Basic commands\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":1786,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"class_list":["post-1681","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1681"}],"version-history":[{"count":13,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1681\/revisions"}],"predecessor-version":[{"id":1695,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1681\/revisions\/1695"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/media\/1786"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1681"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}