{"id":254,"date":"2022-12-23T11:47:52","date_gmt":"2022-12-23T09:47:52","guid":{"rendered":"https:\/\/amsd.go.ro\/wordpress\/?p=254"},"modified":"2022-12-23T11:48:59","modified_gmt":"2022-12-23T09:48:59","slug":"how-to-install-postgresql-on-centos","status":"publish","type":"post","link":"https:\/\/amsd.go.ro\/wordpress\/?p=254","title":{"rendered":"How to Install PostgreSQL on CentOS"},"content":{"rendered":"\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 SQL Server and Oracle.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-first-method-install-postgresql-on-centos-7-using-the-centos-repositories\">Install PostgreSQL on CentOS using the standard repositories<\/h2>\n\n\n\n<p>The CentOS repository contains PostgreSQL. Note that it may not have the latest version of PostgreSQL.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-access-your-server\">1. Access Your Server<\/h3>\n\n\n\n<p>Remember, before starting to install PostgreSQL on CentOS, we need to access our virtual private server with SSH. Check out our <a href=\"https:\/\/www.hostinger.com\/tutorials\/how-to-use-putty-ssh\">PuTTY tutorial<\/a> if you\u2019re having trouble<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-install-postgresql-on-centos-7\">2. Install PostgreSQL on CentOS<\/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\" id=\"h-3-initialize-the-database\">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\" id=\"h-4-start-the-database\">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\" id=\"h-5-optional-enable-postgresql\">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\" id=\"h-postgresql-basic-setup\">PostgreSQL Basic Setup<\/h2>\n\n\n\n<p>In Linux by default, a user named&nbsp;<strong>postgres<\/strong> 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>To change the password, use the below command where you add your new password instead of the <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 <strong>\\h<\/strong>. You can use <strong>\\h<\/strong> followed by the command for which you need more information. To exit the environment you can type <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 <strong>testDB<\/strong> using the&nbsp;<strong>postgres<\/strong> 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 <strong>\\l<\/strong> or <strong>\\list<\/strong> commands to show all the databases. To know the current database you\u2019re using, you can use <strong>\\c<\/strong>. In case you want more information about connections such as the socket, port, etc. &nbsp;then you can use <strong>\\conninfo<\/strong>.<\/p>\n\n\n\n<p>You can also drop or delete a database using the&nbsp;<strong>dropdb<\/strong> 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 <strong>\\dt<\/strong> command. To list all roles, you use the <strong>\\du<\/strong> command. To learn more, we encourage you to read the <a href=\"https:\/\/www.postgresql.org\/download\/linux\/redhat\/\" target=\"_blank\" rel=\"noreferrer noopener\">official documentation<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<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 SQL Server and Oracle. Install PostgreSQL on CentOS using the standard repositories The [&hellip;]<\/p>\n<div class=\"link-more\"><a href=\"https:\/\/amsd.go.ro\/wordpress\/?p=254#more-254\" class=\"more-link\">Continue reading &#10142; <span class=\"screen-reader-text\">How to Install PostgreSQL on CentOS<\/span><\/a><\/div>","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254"}],"version-history":[{"count":2,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":256,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions\/256"}],"wp:attachment":[{"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/amsd.go.ro\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}