{"id":2982,"date":"2020-03-02T15:26:17","date_gmt":"2020-03-02T21:26:17","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=2982"},"modified":"2020-03-02T15:26:17","modified_gmt":"2020-03-02T21:26:17","slug":"how-to-install-postgresql-on-centos-7","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=2982","title":{"rendered":"How to Install PostgreSQL on CentOS 7"},"content":{"rendered":"\n<p>PostgreSQL is a powerful and open source relational database platform. It\u2019s scalable across multiple platforms and is a widely used and well-loved tool. In this tutorial, we\u2019ll show you how to install PostgreSQL on CentOS 7!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Does PostgreSQL work?<\/h2>\n\n\n\n<p>PostgreSQL uses SQL for relational queries and JSON for non-relational queries. One advantage of using PostgreSQL is its immense community support! Its open source nature means that a lot of developers contribute to this utility\u2019s growth.<\/p>\n\n\n\n<p>PostgreSQL is scalable, reliable and accompanied by advanced optimization features. In most cases, people assume advanced optimization and data types are supported by only commercial databases like\u00a0SQL Server\u00a0and Oracle. Contrary to this belief, PostgreSQL provides all of this and many more advanced features, definitely making it a worthwhile addition to your\u00a0VPS.<\/p>\n\n\n\n<p>PostgreSQL is extremely simple to start using and master.<\/p>\n\n\n\n<p>Here we will demonstrate how to install PostgreSQL on CentOS 7.<\/p>\n\n\n\n<p>You can install PostgreSQL on CentOS 7 in using one of two methods:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Install PostgreSQL from existing CentOS repositories<\/li><li>Install from the PostgreSQL repository<\/li><\/ol>\n\n\n\n<p>Let us check each of this method in more detail:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">First Method \u2013 Install PostgreSQL on CentOS 7 using the CentOS repositories<\/h2>\n\n\n\n<p>The CentOS 7 repository contains PostgreSQL. Note that it may not have the latest version of PostgreSQL. At the time of writing the repository hosts PostgreSQL version 9.2.15.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Access Your Server<\/h3>\n\n\n\n<p>Remember, before starting to install PostgreSQL on CentOS 7, we need to access our VPS server with SSH. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Install PostgreSQL on CentOS 7<\/h3>\n\n\n\n<p>It is simple to install PostgreSQL from CentOS 7 repositories. Start with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install postgresql-server postgresql-contrib<\/pre>\n\n\n\n<p>This might take some time to complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Initialize the Database<\/h3>\n\n\n\n<p>Once the installation is done, you can initialize the database using the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo postgresql-setup initdb<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Start the Database<\/h3>\n\n\n\n<p>After initializing the database, you can start the database using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start postgresql<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. (Optional) Enable PostgreSQL<\/h3>\n\n\n\n<p>This completes our database installation and initialization. If required you can configure PostgreSQL to start on every system reboot automatically.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl enable postgresql<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Second Method &nbsp;\u2013 Install PostgreSQL on CentOS 7 using the PostgreSQL repository<\/h2>\n\n\n\n<p>With this method, you can download any latest version of PostgreSQL from the official website. You can select the PostgreSQL version from the PostgreSQL yum repositories. Currently, this shows PostgreSQL version 9.6 as the latest version.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Access Your Server<\/h3>\n\n\n\n<p>As with the previous method, first we have to access our server using SSH.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Download PostgreSQL Using Wget<\/h3>\n\n\n\n<p>Now we will download PostgreSQL version 9.6.3. Similarly, you can download any version. This can be done using the wget command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wget https:\/\/download.postgresql.org\/pub\/repos\/yum\/9.6\/redhat\/rhel-7-x86_64\/pgdg-centos96-9.6-3.noarch.rpm<\/pre>\n\n\n\n<p>This also requires RPM or&nbsp;<a href=\"https:\/\/access.redhat.com\/documentation\/en-us\/red_hat_enterprise_linux\/5\/html\/deployment_guide\/ch-rpm\">RedHat Package manager<\/a>&nbsp;along with EPEL (Extra Packages Enterprise Linux) repositories. This is required for additional PostgreSQL dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Install PosgreSQL on CentOS 7 with the Downloaded Package<\/h3>\n\n\n\n<p>This can be installed using the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install pgdg-centos96-9.6-3.noarch.rpm epel-release<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Update Yum<\/h3>\n\n\n\n<p>Update yum so that your changes get reflected. Use the below command for this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum update<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Complete the PostgreSQL Install Process for CentOS 7<\/h3>\n\n\n\n<p>This completes our prerequisites for PostgreSQL installation. Next, we can install this using the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install postgresql96-server postgresql96-contrib<\/pre>\n\n\n\n<p>This completes our PostgreSQL installation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Initialize the Database<\/h3>\n\n\n\n<p>Next, you can initialize the database using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo \/usr\/pgsql-9.6\/bin\/postgresql96-setup initdb<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. Restart PostgreSQL<\/h3>\n\n\n\n<p>You can restart PostgreSQL using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start postgresql-9.6<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. (Optional) Enable PostgreSQL Launch on Reboot<\/h3>\n\n\n\n<p>In case you want PostgreSQL to start at system reboot automatically then you can optionally use the below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl enable postgresql-9.6<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PostgreSQL Basic Setup<\/h2>\n\n\n\n<p>In Linux by default, a user named&nbsp;<strong>postgres<\/strong>&nbsp;is created once PostgreSQL is installed. You can change the user\u2019s password with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo passwd postgres<\/pre>\n\n\n\n<p>You will be prompted to enter the new password twice.<\/p>\n\n\n\n<p>Next, you can switch to the PostgreSQL prompt and change the password for the PostgreSQL postgres user using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">su - postgres<\/pre>\n\n\n\n<p>If you receive an error, you can set a valid shell on the user with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">su --shell \/bin\/bash postgres<\/pre>\n\n\n\n<p>Afterwards, perform the same command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">su - postgres<\/pre>\n\n\n\n<p>To change the password, use the below command where you add your new password instead of the&nbsp;<strong>NewPassword<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">psql -d template1 -c \"ALTER USER postgres WITH PASSWORD 'NewPassword';\"<\/pre>\n\n\n\n<p>You can switch to the PostgreSQL client shell using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">psql postgres<\/pre>\n\n\n\n<p>Here you can check the list of available commands by typing&nbsp;<strong>\\h<\/strong>. You can use&nbsp;<strong>\\h<\/strong>&nbsp;followed by the command for which you need more information. To exit the environment you can type&nbsp;<strong>\\q<\/strong>.<\/p>\n\n\n\n<p>The createdb command lets you create new databases. Suppose we want to create a new database named&nbsp;<strong>testDB<\/strong>&nbsp;using the&nbsp;<strong>postgres<\/strong>&nbsp;Linux user. The command we would use would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">createdb testDB<\/pre>\n\n\n\n<p>You can create a new role using the createuser command. Below is an example where we are creating a role named samplerole using the postgres Linux user.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">createuser samplerole \u2013pwprompt<\/pre>\n\n\n\n<p>Here you will be prompted to set a password for the user.<\/p>\n\n\n\n<p>Optionally you can assign the ownership of our newly created database to a specific postgres user or role. This can be done with a command like this one:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">createdb testDB -O samplerole<\/pre>\n\n\n\n<p>In the above command, replace samplerole with the role you want to use.<\/p>\n\n\n\n<p>You can connect to this new database using the command bellow:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">psql testDB<\/pre>\n\n\n\n<p>In case you want to use a specific user or role to log in, use the command as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">psql testDB -U samplerole<\/pre>\n\n\n\n<p>This will prompt you to enter the password.<\/p>\n\n\n\n<p>You can use&nbsp;<strong>\\l<\/strong>&nbsp;or&nbsp;<strong>\\list<\/strong>&nbsp;commands to show all the databases. To know the current database you\u2019re using, you can use&nbsp;<strong>\\c<\/strong>. In case you want more information about connections such as the socket, port, etc. &nbsp;then you can use&nbsp;<strong>\\conninfo<\/strong>.<\/p>\n\n\n\n<p>You can also drop or delete a database using the&nbsp;<strong>dropdb<\/strong>&nbsp;command. However, remember to verify what you\u2019re deleting before doing it. Deleted databases cannot be retrieved.<\/p>\n\n\n\n<p>To delete a database, you can use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dropdb testDB<\/pre>\n\n\n\n<p>PostgreSQL similar to other databases allows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Table creation<\/li><li>Table deletion<\/li><li>Table Updates<\/li><li>Column Addition<\/li><li>Drop column<\/li><li>Query a table<\/li><li>Alter commands<\/li><li>Grant Privileges<\/li><\/ul>\n\n\n\n<p>The syntax for all of these commands is similar to most database commands. You can list all the tables by using the&nbsp;<strong>\\dt<\/strong>&nbsp;command. To list all roles, you use the&nbsp;<strong>\\du<\/strong>&nbsp;command. To learn more, we encourage you to read the&nbsp;<a href=\"https:\/\/www.postgresql.org\/download\/linux\/redhat\/\">official documentation<\/a>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>With this, we have shown you PostgreSQL installation in CentOS 7 and also walked you through the basic PostgreSQL setup. There are several things that can still be learned in PostgreSQL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL is a powerful and open source relational database platform. It\u2019s scalable across multiple platforms and is a widely used and well-loved tool. In this tutorial, we\u2019ll show you how to install PostgreSQL on CentOS 7! How Does PostgreSQL work? PostgreSQL uses SQL for relational queries and JSON for non-relational queries. One advantage of using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-2982","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2982"}],"version-history":[{"count":1,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2982\/revisions"}],"predecessor-version":[{"id":2983,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2982\/revisions\/2983"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}