{"id":16068,"date":"2019-03-19T13:45:35","date_gmt":"2019-03-19T13:45:35","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=16068"},"modified":"2026-03-10T10:25:02","modified_gmt":"2026-03-10T10:25:02","slug":"how-to-install-postgresql-on-centos","status":"publish","type":"post","link":"\/in\/tutorials\/how-to-install-postgresql-on-centos","title":{"rendered":"How to install PostgreSQL on CentOS 9 + create roles and databases"},"content":{"rendered":"<p>PostgreSQL is an open-source relational database management system (RDBMS) known for its capability to handle large amounts of data, making it a popular choice for large-scale applications.<\/p><p>It&rsquo;s also compatible with various data types, programming languages, web stacks, and operating systems, including popular Linux distributions like CentOS.<\/p><p>In this tutorial, we will explain how to install PostgreSQL on CentOS 9. You will also learn basic <strong>postgres <\/strong>statements and commands for managing roles and databases.<\/p><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-prerequisites-for-installing-postgresql-on-centos\">Prerequisites for installing PostgreSQL on CentOS<\/h2><p>Before installing PostgreSQL, make sure your server meets the recommended hardware requirements:<\/p><ul class=\"wp-block-list\">\n<li>1 GHz processor.<\/li>\n\n\n\n<li>At least 2 GB of RAM.<\/li>\n\n\n\n<li>Minimum 512 MB of storage space.&nbsp;<\/li>\n<\/ul><p>Your system must also run CentOS 9 since the installation steps might differ otherwise.<\/p><p>If you don&rsquo;t have a server, we recommend purchasing a <a href=\"\/in\/vps\/centos-hosting\">Hostinger CentOS VPS hosting plan<\/a>. The<strong> <\/strong>KVM 1 plan, starting at <strong>\u20b9599.00\/month<\/strong>, provides <strong>1 vCPU<\/strong> core, <strong>4 GB <\/strong>RAM, and <strong>50 GB <\/strong>of storage, which is more than suitable for hosting PostgreSQL.<\/p><p>Plus, you can easily install various Linux operating systems on Hostinger VPS without any commands &ndash; simply select an OS template. We actively update these templates to add support for the latest distros, including <strong>CentOS 9<\/strong>.<\/p><p>Hostinger VPS plans also streamline your PostgreSQL database management tasks. Our free weekly automatic backup and built-in malware scanner will help you maintain data integrity without having to spend any time on it.<\/p><p>If you need help managing your server, ask <strong>Kodee<\/strong><span style=\"margin: 0px;padding: 0px\"><strong>,<\/strong>&nbsp;our<strong>&nbsp;AI assistant<\/strong>. With a few simple prompts, you&rsquo;ll get guidance on anything you want to do, including commands<\/span>.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner-1024x300.png\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2023\/02\/VPS-hosting-banner-768x225.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-how-to-install-and-create-a-postgresql-database-on-centos-9\">How to install and create a PostgreSQL database on CentOS 9<\/h2><p>Here are the steps to install PostgreSQL on CentOS. Before proceeding, connect to your server <a href=\"\/in\/tutorials\/how-to-use-putty-ssh\">using an SSH client like PuTTY<\/a>, Terminal, or Hostinger&rsquo;s <strong>Browser terminal<\/strong>.<\/p><h3 class=\"wp-block-heading\" id=\"h-1-download-and-install-postgresql\">1. Download and install PostgreSQL<\/h3><p>You can install the PostgreSQL package in two ways &ndash; via the official or local repository. In this tutorial, we will use the official repo since it updates more quickly than the local one. Here are the steps:<\/p><ol class=\"wp-block-list\">\n<li>Add the PostgreSQL repository to your system using the following command:<\/li>\n<\/ol><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=\"\">sudo dnf install -y https:\/\/download.postgresql.org\/pub\/repos\/yum\/reporpms\/EL-9-x86_64\/pgdg-redhat-repo-latest.noarch.rpm<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Disable your system&rsquo;s built-in PostgreSQL module to avoid installing the wrong version:<\/li>\n<\/ol><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=\"\">sudo dnf -qy module disable postgresql<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Install the PostgreSQL server by running this command. If you want to set up another version, replace <strong>17 <\/strong>with the corresponding number:<\/li>\n<\/ol><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=\"\">sudo dnf install -y postgresql17-server<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Initialize the database to set up the necessary files and configuration for PostgreSQL:<\/li>\n<\/ol><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=\"\">sudo \/usr\/pgsql-17\/bin\/postgresql-17-setup initdb<\/pre><ol start=\"5\" class=\"wp-block-list\">\n<li>Start and enable PostgreSQL during startup by running these commands subsequently:<\/li>\n<\/ol><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=\"\">sudo systemctl enable postgresql-17\n\nsudo systemctl start postgresql-17<\/pre><p>And that&rsquo;s it! Before accessing the database, make sure PostgreSQL is running by checking its status using this command:<\/p><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=\"\">sudo systemctl status postgresql-17<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69dfbedc3ffad\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"86\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/systemctl-shows-postgresql-service-status-1024x86.png\" alt=\"Systemctl shows postgresql service status\" class=\"wp-image-119233\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/systemctl-shows-postgresql-service-status-1024x86.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/systemctl-shows-postgresql-service-status-300x25.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/systemctl-shows-postgresql-service-status-150x13.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/systemctl-shows-postgresql-service-status-768x65.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/systemctl-shows-postgresql-service-status.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Alternatively, if you use Hostinger VPS, you can check the status of Postgresql without command by asking the <a href=\"\/blog\/kodee\">Kodee AI assistant<\/a>. Simply enter, &ldquo;Check the postgresql-17 service status in my VPS&rdquo; and Kodee will show information about the service. <\/p><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69dfbedc41650\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"319\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/kodee-checks-the-postgresql-17-service-status-1024x319.png\" alt=\"Kodee checks the postgresql-17 service status \" class=\"wp-image-124633\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/kodee-checks-the-postgresql-17-service-status-1024x319.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/kodee-checks-the-postgresql-17-service-status-300x93.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/kodee-checks-the-postgresql-17-service-status-150x47.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/kodee-checks-the-postgresql-17-service-status-768x239.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/kodee-checks-the-postgresql-17-service-status.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-2-connect-to-the-postgresql-shell\">2. Connect to the PostgreSQL shell<\/h3><p>To manage your database, you must connect to the PostgreSQL or <strong>psql <\/strong>command-line shell. You can do so by switching to the <strong>postgres<\/strong> root user, which comes pre-configured during the installation:<\/p><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=\"\">sudo su postgres<\/pre><p>Your shell prompt should change. Now, enter the PostgreSQL shell using this command:<\/p><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=\"\">psql<\/pre><p>Once your command line changes into <strong>Postgres=#<\/strong>, you can manage your database. To quit the PostgreSQL prompt and return to your system&rsquo;s main shell, enter the following:<\/p><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=\"\">q<\/pre><h3 class=\"wp-block-heading\" id=\"h-3-create-a-new-role\">3. Create a new role<\/h3><p>A <a href=\"https:\/\/www.postgresql.org\/docs\/current\/user-manag.html\" target=\"_blank\" rel=\"noopener\">role in PostgreSQL<\/a> is identical to a <strong>user<\/strong> or <strong>group <\/strong>&ndash; an entity that you can use to manage your databases and access permissions in PostgreSQL. To set one up, use the following statement:<\/p><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=\"\">CREATE ROLE my_role;<\/pre><p>Since we didn&rsquo;t set a password, the above statement will create a new role without a login privilege. Think of this role as similar to groups, which are useful for managing privileges.<\/p><p>If you want to make a role with a login permission similar to a user, use the following command:<\/p><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=\"\">CREATE ROLE my_role WITH LOGIN PASSWORD 'my_password';<\/pre><p>When creating a role, you can add the permissions afterward using the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/sql-grant.html\" target=\"_blank\" rel=\"noopener\"><strong>GRANT<\/strong> command<\/a>. Alternatively, you can use the interactive setup to assign the privileges by answering questions.<\/p><p>To do so, quit your PostgreSQL shell and switch back to the <strong>postgres<\/strong> user. Then, run this command:<\/p><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=\"\">createuser --interactive<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69dfbedc42f19\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"156\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/postgresql-interactive-role-creation-1024x156.png\" alt=\"PostgreSQL interactive role creation\" class=\"wp-image-119234\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/postgresql-interactive-role-creation-1024x156.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/postgresql-interactive-role-creation-300x46.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/postgresql-interactive-role-creation-150x23.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/postgresql-interactive-role-creation-768x117.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/postgresql-interactive-role-creation.png 1140w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Simply answer the questions to set up a role based on your needs. To check all roles in your database, run the following command in the psql shell:<\/p><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=\"\">du<\/pre><p>If you want to enter the <strong>postgres <\/strong>shell using that role, you must also create a corresponding new Linux user in your system. Here&rsquo;s the command:<\/p><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=\"\">sudo adduser new-user<\/pre><p>Connecting to<strong> psql <\/strong>as the new role also requires a database. If the user doesn&rsquo;t have one, you can use the default <strong>postgres <\/strong>database. The command might look like this:<\/p><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=\"\">sudo -u new-user psql -d postgres&nbsp;<\/pre><p><div><p class=\"important\"><strong>Important!<\/strong> To access the psql command line as another user, run the login command in your system&rsquo;s main shell.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-4-make-a-new-database\">4. Make a new database<\/h3><p>To create a new PostgreSQL database, log in to the <strong>psql<\/strong> shell as a user with the necessary permissions.<\/p><p>Now, create a new database by running the following command.<\/p><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=\"\">CREATE DATABASE db_name;<\/pre><p>List all databases to verify that you have successfully created a new one. Here&rsquo;s the command:<\/p><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=\"\">l<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69dfbedc4475b\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"172\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/database-list-on-postgresql-1024x172.png\" alt=\"\" class=\"wp-image-119235\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/database-list-on-postgresql-1024x172.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/database-list-on-postgresql-300x50.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/database-list-on-postgresql-150x25.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/database-list-on-postgresql-768x129.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/database-list-on-postgresql.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><p>Now, connect to the new database by running the following command. Replace <strong>db_name <\/strong>with the actual name:<\/p><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=\"\">c db_name<\/pre><p>Once connected, you can check information about the current database by entering this:<\/p><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=\"\">conninfo<\/pre><h3 class=\"wp-block-heading\" id=\"h-5-add-and-remove-a-database-table\">5. Add and remove a database table<\/h3><p>RDBMS, like PostgreSQL, organizes data into tables consisting of rows and columns. For more efficient management, you can group related tables into a category called <strong>schema<\/strong>.<\/p><p>For example, you can organize tables about employee data and payroll invoices into a human resource (HR) schema. To create a schema, use the following statement:<\/p><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=\"\">CREATE SCHEMA schema_name;<\/pre><p>To create a table within the schema, simply add the schema name at the beginning of your statement like so:<\/p><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=\"\">CREATE TABLE schema_name.table_name (\n\n&nbsp;&nbsp;&nbsp;&nbsp;column1 data_type constraints,\n\n&nbsp;&nbsp;&nbsp;&nbsp;column2 data_type constraints,\n\n&nbsp;&nbsp;&nbsp;&nbsp;...\n\n);<\/pre><p>When creating a table, list the columns separated by a comma. Each column should contain a <a href=\"https:\/\/www.postgresql.org\/docs\/current\/datatype.html\" target=\"_blank\" rel=\"noopener\"><strong>data type<\/strong><\/a> to specify values to insert and a <a href=\"https:\/\/www.postgresql.org\/docs\/current\/ddl-constraints.html\" target=\"_blank\" rel=\"noopener\"><strong>constraint<\/strong><\/a> to define the data criteria.<\/p><p>For example, you can set the data type to integers, characters, or boolean values. Meanwhile, you can set a constraint to permit only non-empty values or unique data that other columns don&rsquo;t have.<\/p><p>Here&rsquo;s a statement example that creates an employee data table inside the HR schema, containing four columns about IDs, first and last names, as well as hiring date:<\/p><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=\"\">CREATE TABLE hr.employees (\n\n&nbsp;&nbsp;&nbsp;&nbsp;employee_id SERIAL PRIMARY KEY,\n\n&nbsp;&nbsp;&nbsp;&nbsp;first_name VARCHAR(100) NOT NULL,&nbsp;\n\n&nbsp;&nbsp;&nbsp;&nbsp;last_name VARCHAR(100) NOT NULL,\n\n&nbsp;&nbsp;&nbsp;&nbsp;hire_date DATE NOT NULL&nbsp;&nbsp;\n\n);<\/pre><p>To check the table, enter the following in your <strong>psql <\/strong>command-line shell:<\/p><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=\"\">d table-name<\/pre><p>If you want to check tables belonging to another schema, use this command instead:<\/p><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=\"\">dt schema-name.table<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69dfbedc45ee6\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img decoding=\"async\" width=\"1024\" height=\"187\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/03\/a-postgresql-database-table-1024x187.png\" alt=\"A PostgreSQL database table\" class=\"wp-image-119236\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/a-postgresql-database-table-1024x187.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/a-postgresql-database-table-300x55.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/a-postgresql-database-table-150x27.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/a-postgresql-database-table-768x140.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2019\/03\/a-postgresql-database-table.png 1460w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><h3 class=\"wp-block-heading\" id=\"h-6-insert-update-and-delete-data-in-a-table\">6. Insert, update, and delete data in a table<\/h3><p>After setting up a table and columns, you can start populating them with data. To do so, specify the table and columns you want to insert the data into, then list the values like so:<\/p><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=\"\">INSERT INTO hr.employees (first_name, last_name, hire_date)\n\nVALUES\n\n&nbsp;&nbsp;&nbsp;&nbsp;('John', 'Doe', '2024-01-15'),\n\n&nbsp;&nbsp;&nbsp;&nbsp;('Jane', 'Smith', '2023-11-10');<\/pre><p>In the example, we insert values into <strong>employees<\/strong> table&rsquo;s first name, last name, and hiring date column. The first row will have data about <strong>John Doe<\/strong>, while the second contains details about <strong>Jane Smith<\/strong>.<\/p><p>To check the value of a specific row, you can use a particular condition as a filter. For example, the following will look up an employee using their ID:<\/p><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=\"\">SELECT * FROM employees\n\nWHERE employee_id = 1;<\/pre><p>You can also use conditions to update a specific row&rsquo;s value. For example, this statement will change Jane Smith to Jane Doe:<\/p><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=\"\">UPDATE employees\n\nSET last_name = 'Doe'\n\nWHERE employee_id = 2;<\/pre><p>Moreover, conditions are useful for deleting data. For instance, we will delete data about an employee with the ID of 1:<\/p><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=\"\">DELETE FROM employees\n\nWHERE employee_id = 1;<\/pre><p>Deleting a row will completely remove the entry from your table. Doing so won&rsquo;t affect neighboring data unless they have specific constraints, like the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/ddl-constraints.html#DDL-CONSTRAINTS-FK\" target=\"_blank\" rel=\"noopener\">foreign key relationship<\/a>.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>In this article, we have explained how to install PostgreSQL on CentOS 9. Before setting it up, make sure you have a server that meets the minimum system requirements, like Hostinger&rsquo;s KVM 1 VPS plan.<\/p><p>Once you have the system, install PostgreSQL using the following steps:<\/p><ol class=\"wp-block-list\">\n<li>Download the latest PostgreSQL package and enable its service using <strong>systemctl<\/strong>.&nbsp;<\/li>\n\n\n\n<li>Connect to the PostgreSQL shell by switching to the <strong>postgres <\/strong>account and entering <strong>psql<\/strong>.<\/li>\n\n\n\n<li>Create a new role in your database and Linux system with login permission.<\/li>\n\n\n\n<li>Make a new database using a role with sufficient permission, like <strong>postgres<\/strong>.<\/li>\n\n\n\n<li>Add a table to organize data in your database by entering the <strong>CREATE<\/strong> statement.&nbsp;<\/li>\n\n\n\n<li>Populate and manage data in your database tables using statements like <strong>INSERT<\/strong> and <strong>UPDATE<\/strong>.<\/li>\n<\/ol><p>We hope this article helped you set up a PostgreSQL database in your CentOS 9 system. If you have any questions or issues, leave us a comment below.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-install-postgresql-on-centos\">How to install PostgreSQL on CentOS <\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1732537664359\"><h3 class=\"schema-faq-question\">How do I verify whether PostgreSQL is installed correctly?<\/h3> <p class=\"schema-faq-answer\">There are a couple of ways to check whether PostgreSQL is installed. One is by starting the database using the <strong>systemctl<\/strong> command:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo systemctl start postgresql-17<\/code><br>Make sure to replace <strong>17<\/strong> with the actual version you installed. If your command line returns an error about a missing service, it means PostgreSQL isn&rsquo;t configured properly.&nbsp;<br>Another method is to log in to the PostgreSQL command line using the following:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo -u postgres psql<\/code><br>If you see the <strong>Command not found <\/strong>error, it means the RDBMS isn&rsquo;t correctly installed.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1732537698184\"><h3 class=\"schema-faq-question\">How do I access the PostgreSQL command line interface?<\/h3> <p class=\"schema-faq-answer\">To access the PostgreSQL command-line interface, also known as the <strong>psql<\/strong> shell, switch to the default<strong> postgres <\/strong>account using this command:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo su postgres<\/code><br>Now, simply enter <strong>psql<\/strong>,and your shell should change to PostgreSQL, indicating that you have accessed the command-line interface. To quit the shell, enter the following:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">q<\/code><br>You can also use a single command in your main shell to enter <strong>psql<\/strong> like so:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo -u postgres psql<\/code><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1732537733932\"><h3 class=\"schema-faq-question\">Why do I encounter issues when installing PostgreSQL?<\/h3> <p class=\"schema-faq-answer\">Several factors can cause issues during the PostgreSQL installation. If you encounter errors when downloading the package, check if your command is correct and uses the right download URL according to your operating system.<br>Should an issue arise when connecting to the <strong>psql <\/strong>shell, check whether the <strong>postgresql<\/strong> service is running using this command:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo systemctl status postgresql-17<\/code><br>If the PostgreSQL status is not running, manually boot it using this command:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo systemctl start postgresql-17<\/code><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1732537769844\"><h3 class=\"schema-faq-question\">Why can&rsquo;t I connect to another PostgreSQL role?<\/h3> <p class=\"schema-faq-answer\">If you can&rsquo;t connect to a new PostgreSQL role, make sure you have created the corresponding user in your LInux system. For example, if you set up the <strong>new_user<\/strong> role, your server must also have the same <strong>new_user <\/strong>account.&nbsp;<br>In addition, you must specify the database to connect. If the new user doesn&rsquo;t have one, use the default <strong>postgres <\/strong>database like this command:<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">sudo -u NewUser psql -d postgres<\/code><br>Moreover, try creating the new role using the following statement instead of the interactive mode to grant the login privilege explicitly.&nbsp;<br><code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">CREATE ROLE my_role WITH LOGIN PASSWORD 'my_password'<\/code><\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL is an open-source relational database management system (RDBMS) known for its capability to handle large amounts of data, making it a popular choice for large-scale applications. It&rsquo;s also compatible with various data types, programming languages, web stacks, and operating systems, including popular Linux distributions like CentOS. In this tutorial, we will explain how to [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/how-to-install-postgresql-on-centos\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":83491,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[22642,22640],"tags":[],"class_list":["post-16068","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pre-installed-applications","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/instalar-postgresql-centos","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/installer-postgresql-centos","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-instalar-postgresql-en-centos","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/cara-instal-postgresql-centos","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/como-instalar-postgresql-en-centos","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/como-instalar-postgresql-en-centos","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-instalar-postgresql-en-centos","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/instalar-postgresql-centos","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-install-postgresql-on-centos","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-install-postgresql-on-centos","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/16068","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=16068"}],"version-history":[{"count":42,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/16068\/revisions"}],"predecessor-version":[{"id":124635,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/16068\/revisions\/124635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media\/83491"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=16068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=16068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=16068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}