{"id":408,"date":"2021-03-25T14:24:03","date_gmt":"2021-03-25T17:24:03","guid":{"rendered":"http:\/\/xaxowareti.com.br\/?p=408"},"modified":"2021-03-25T14:24:05","modified_gmt":"2021-03-25T17:24:05","slug":"how-to-install-postgresql-on-debian-9","status":"publish","type":"post","link":"https:\/\/xaxowareti.com.br\/?p=408","title":{"rendered":"How to Install PostgreSQL on Debian 9"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">How to Install PostgreSQL on Debian 9<\/h1>\n\n\n\n<p>Updated&nbsp;&nbsp;Feb 13, 2019\u2022<\/p>\n\n\n\n<p>5 min read<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/linuxize.com\/post\/how-to-install-postgresql-on-debian-9\/featured_hu3b22226b9b0d7d318005ec26e340ddcb_33785_768x0_resize_q75_lanczos.jpg?ezimgfmt=ng%3Awebp%2Fngcb87%2Frs%3Adevice%2Frscb87-1\" alt=\"Install PostgreSQL on Debian 9\"\/><\/figure>\n\n\n\n<p>PostgreSQL, often known simply as Postgres, is an open-source general-purpose object-relational database management system. PostgreSQL has many advanced features such as online backups, point in time recovery, nested transactions, SQL and JSON querying, multi-version concurrency control (MVCC), asynchronous replication and more.<\/p>\n\n\n\n<p>In this tutorial, we will show you how to install PostgreSQL on Debian 9 and explore the fundamentals of basic database administration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"prerequisites\">Prerequisites<\/h2>\n\n\n\n<p>Before proceeding with this tutorial, make sure the user you are logged in as has&nbsp;<a href=\"https:\/\/linuxize.com\/post\/how-to-create-a-sudo-user-on-debian\/\">sudo privileges<\/a>&nbsp;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-postgresql\">Installing PostgreSQL<\/h2>\n\n\n\n<p>At the time of writing this article, the latest version of PostgreSQL available from the Debian repositories is PostgreSQL version 9.6.<\/p>\n\n\n\n<p>To install PostgreSQL on your Debian server complete the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Begin by updating the local package index:<code>sudo apt update<\/code>Copy<\/li><li>Install the PostgreSQL server and PostgreSQL contrib package which provides additional features for the PostgreSQL database:<code>sudo apt install postgresql postgresql-contrib<\/code>Copy<\/li><li>When the installation is completed, the PostgreSQL service will start automatically. To verify the installation we\u2019ll connect to the PostgreSQL database server using the&nbsp;<code>psql<\/code>&nbsp;utility and print the&nbsp;<a href=\"https:\/\/linuxize.com\/post\/how-to-check-postgresql-version\/\">server version<\/a>&nbsp;:<code>sudo -u postgres psql -c \"SELECT version();\"<\/code>CopyThe output will look like this:<code> version ----------------------------------------------------------------------------------------------------------- PostgreSQL 9.6.10 on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit (1 row)<\/code>Copy<\/li><\/ol>\n\n\n\n<p>Psql is an interactive terminal program that allows you to interact with the PostgreSQL server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"postgresql-roles-and-authentication-methods\">PostgreSQL Roles and Authentication Methods<\/h2>\n\n\n\n<p>PostgreSQL handles database access permissions using the concept of roles. A role can represent a database user or a group of database users.<\/p>\n\n\n\n<p>PostgreSQL supports a number of&nbsp;<a href=\"https:\/\/www.postgresql.org\/docs\/10\/static\/auth-methods.html\" target=\"_blank\" rel=\"noreferrer noopener\">authentication methods<\/a>&nbsp;. The most commonly used methods are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Trust &#8211; With this method, the role can connect without a password, as long as the criteria defined in the&nbsp;<code>pg_hba.conf<\/code>&nbsp;are met.<\/li><li>Password &#8211; A role can connect by providing a password. The passwords can be stored as&nbsp;<code>scram-sha-256<\/code>&nbsp;<code>md5<\/code>&nbsp;and&nbsp;<code>password<\/code>&nbsp;(clear-text)<\/li><li>Ident &#8211; This method is only supported on TCP\/IP connections. Works by obtaining the client\u2019s operating system user name, with an optional user name mapping.<\/li><li>Peer &#8211; Same as Ident but it is only supported on local connections.<\/li><\/ul>\n\n\n\n<p>PostgreSQL client authentication is defined in the configuration file named&nbsp;<code>pg_hba.conf<\/code>. By default for local connections, PostgreSQL is set to use the peer authentication method.<\/p>\n\n\n\n<p>The&nbsp;<code>postgres<\/code>&nbsp;user is created automatically when you install PostgreSQL. This user is the superuser for the PostgreSQL instance and it is equivalent to the MySQL root user.<\/p>\n\n\n\n<p>To log in to the PostgreSQL server as the postgres user first you need to&nbsp;<a href=\"https:\/\/linuxize.com\/post\/su-command-in-linux\/\">switch to the user<\/a>&nbsp;postgres and then you can access a PostgreSQL prompt using the&nbsp;<code>psql<\/code>&nbsp;utility:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo su - postgres<\/code><code>psql<\/code>CopyCopy<\/pre>\n\n\n\n<p>From here, you can interact with your PostgreSQL instance. To exit out of the PostgreSQL shell type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\\q\n<\/code><\/pre>\n\n\n\n<p>You can use the&nbsp;<code>sudo<\/code>&nbsp;command to access the PostgreSQL prompt without switching users:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo -u postgres psql<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>postgres<\/code>&nbsp;user is typically used only from the local host and it is recommended not to set the password for this user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-postgresql-role-and-database\">Creating PostgreSQL Role and Database<\/h2>\n\n\n\n<p>You can create new roles from the command line using the&nbsp;<code>createuser<\/code>&nbsp;command. Only superusers and roles with&nbsp;<code>CREATEROLE<\/code>&nbsp;privilege can create new roles.<\/p>\n\n\n\n<p>In the following example, we will create a new role named&nbsp;<code>john<\/code>&nbsp;a database named&nbsp;<code>johndb<\/code>&nbsp;and grant privileges on the database.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a new PostgreSQL RoleThe following command will create a new role named \u201cjohn\u201d:<code>sudo su - postgres -c \"createuser john\"<\/code>Copy<\/li><li>Create a new PostgreSQL DatabaseCreate a new database named \u201cjohndb\u201d using the&nbsp;<code>createdb<\/code>&nbsp;command:<code>sudo su - postgres -c \"createdb johndb\"<\/code>Copy<\/li><li>Grant privilegesTo grant permissions to the&nbsp;<code>john<\/code>&nbsp;user on the database we created in the previous step, connect to the PostgreSQL shell:<code>sudo -u postgres psql<\/code>Copyand run the following query:<code><strong>GRANT<\/strong> <strong>ALL<\/strong> <strong>PRIVILEGES<\/strong> <strong>ON<\/strong> <strong>DATABASE<\/strong> johndb <strong>TO<\/strong> john;<\/code>Copy<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enable-remote-access-to-postgresql-server\">Enable remote access to PostgreSQL server<\/h2>\n\n\n\n<p>By default the PostgreSQL, server listens only on the local interface&nbsp;<code>127.0.0.1<\/code>. To enable remote access to your PostgreSQL server open the configuration file&nbsp;<code>postgresql.conf<\/code>&nbsp;and add&nbsp;<code>listen_addresses = '*'<\/code>&nbsp;in the&nbsp;<code>CONNECTIONS AND AUTHENTICATION<\/code>&nbsp;section.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/postgresql\/9.6\/main\/postgresql.conf<\/code><\/pre>\n\n\n\n<p>\/etc\/postgresql\/9.6\/main\/postgresql.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#------------------------------------------------------------------------------\n# CONNECTIONS AND AUTHENTICATION\n#------------------------------------------------------------------------------\n\n# - Connection Settings -\n\nlisten_addresses = '*'     # what IP address(es) to listen on;\n<\/code><\/pre>\n\n\n\n<p>save the file and restart the PostgreSQL service with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service postgresql restart<\/code><\/pre>\n\n\n\n<p>Verify the changes with the&nbsp;<code>ss<\/code>&nbsp;utility:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ss -nlt | grep 5432<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>LISTEN   0         128                 0.0.0.0:5432             0.0.0.0:*\nLISTEN   0         128                    &#91;::]:5432                &#91;::]:*\n<\/code><\/pre>\n\n\n\n<p>As you can see from the output above the PostgreSQL server is&nbsp;<a href=\"https:\/\/linuxize.com\/post\/check-listening-ports-linux\/\">listening<\/a>&nbsp;on all interfaces (0.0.0.0).<\/p>\n\n\n\n<p>The last step is to configure the server to accept remote connections by editing the&nbsp;<code>pg_hba.conf<\/code>&nbsp;file.<\/p>\n\n\n\n<p>Below are some examples showing different use cases:\/etc\/postgresql\/9.6\/main\/pg_hba.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># TYPE  DATABASE        USER            ADDRESS                 METHOD\n\n# The user jane will be able to access all databases from all locations using a md5 password\nhost    all             jane            0.0.0.0\/0                md5\n\n# The user jane will be able to access only the janedb from all locations using a md5 password\nhost    janedb          jane            0.0.0.0\/0                md5\n\n# The user jane will be able to access all databases from a trusted location (192.168.1.134) without a password\nhost    all             jane            192.168.1.134            trust\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>You have learned how to install and configure PostgreSQL on your Debian 9 server. For more information on this topic, consult the&nbsp;<a href=\"https:\/\/www.postgresql.org\/docs\/9.6\/static\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL Documentation<\/a>&nbsp;.<\/p>\n\n\n\n<p>If you have any questions, please leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Install PostgreSQL on Debian 9 Updated&nbsp;&nbsp;Feb 13, 2019\u2022 5 min read PostgreSQL, often known simply as Postgres, is an open-source general-purpose object-relational database management system. PostgreSQL has many advanced features such as online backups, point in time recovery, nested transactions, SQL and JSON querying, multi-version concurrency control (MVCC), asynchronous replication and more. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-sem-categoria"],"_links":{"self":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=408"}],"version-history":[{"count":1,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":409,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/408\/revisions\/409"}],"wp:attachment":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}