Debian 10安装PostgreSQL 12的方法
<p>添加PostgreSql官方仓储</p><div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># Create the file repository configuration:
sudo sh </span>-c <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
# Import the repository signing key:
wget </span>--quiet -O - https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -</span>
<span style="color: rgba(0, 0, 0, 1)">
# Update the package lists:
sudo apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span><span style="color: rgba(0, 0, 0, 1)"> update
# Install the latest version of PostgreSQL.
# If you want a specific version, use </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">postgresql-12</span><span style="color: rgba(128, 0, 0, 1)">'</span> or similar instead of <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">postgresql</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
sudo apt</span>-<span style="color: rgba(0, 0, 255, 1)">get</span> -y install postgresql</pre>
</div>
<p>如果想安装postgresql 12版本 </p>
<div class="cnblogs_code">
<pre>apt-<span style="color: rgba(0, 0, 255, 1)">get</span> install postgresql-<span style="color: rgba(128, 0, 128, 1)">12</span></pre>
</div>
<p>安装PostgreSQL服务器和contrib软件包,该软件包为PostgreSQL数据库提供附加功能:</p>
<div class="cnblogs_code">
<pre>sudo apt install postgresql postgresql-contrib</pre>
</div>
<p>安装完成后,PostgreSQL服务将启动,要验证安装,请使用psql工具打印服务器版本</p>
<div class="cnblogs_code">
<pre>sudo -u postgres psql -c <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">SELECT version();</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>输出应类似于以下内容:</p>
<div class="cnblogs_code">
<pre>PostgreSQL <span style="color: rgba(128, 0, 128, 1)">12.4</span> (Debian <span style="color: rgba(128, 0, 128, 1)">12.4</span>-<span style="color: rgba(128, 0, 128, 1)">1</span>.pgdg100+<span style="color: rgba(128, 0, 128, 1)">1</span>) on aarch64-unknown-linux-gnu, compiled by gcc (Debian <span style="color: rgba(128, 0, 128, 1)">8.3</span>.<span style="color: rgba(128, 0, 128, 1)">0</span>-<span style="color: rgba(128, 0, 128, 1)">6</span>) <span style="color: rgba(128, 0, 128, 1)">8.3</span>.<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">64</span>-bit</pre>
</div>
<p><strong>启用对PostgreSQL服务器的远程访问</strong></p>
<p>默认情况下,PostgreSQL服务器仅在本地接口127.0.0.1上侦听。</p>
<p>如果要从远程位置连接到PostgreSQL服务器,则需要将服务器设置为在公共接口上侦听,并编辑配置以接受远程连接。</p>
<p>1,打开配置文件postgresql.conf,并在“CONNECTIONS AND AUTHENTICATION”部分中添加listen_addresses = '*',这指示服务器在所有网络接口上进行侦听:</p>
<div class="cnblogs_code">
<pre>sudo nano /etc/postgresql/<span style="color: rgba(128, 0, 128, 1)">12</span>/main/postgresql.conf</pre>
</div>
<p>编辑为如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># CONNECTIONS AND AUTHENTICATION
# </span>- Connection Settings -<span style="color: rgba(0, 0, 0, 1)">
listen_addresses </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">*</span><span style="color: rgba(128, 0, 0, 1)">'</span> # what IP address(es) to listen on;</pre>
</div>
<p>2,打开配置文件pg_hba.conf,将服务器配置为接受远程登录,找到“# IPv4 local connections:” </p>
<div class="cnblogs_code">
<pre>host all all <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">1.210</span>/<span style="color: rgba(128, 0, 128, 1)">32</span> trust</pre>
</div>
<p>或者运行全部IP访问</p>
<div class="cnblogs_code">
<pre>host all all <span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span>/<span style="color: rgba(128, 0, 128, 1)">0</span> trust</pre>
</div>
<p>这里默认是md5,需要设置密码。如果不需要密码连接,需要改为trust,默认安装好后postgres用户是没有密码的 可以设置密码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">sudo -u postgres psql
ALTER Role postgres WITH PASSWORD </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">fofia123</span><span style="color: rgba(128, 0, 0, 1)">'; 注意命令后面又分号<br></span></pre>
</div>
<p><strong>如果有多个可以指定某个实例 </strong></p>
<div class="cnblogs_code">
<pre>sudo -u postgres psql -p <span style="color: rgba(128, 0, 128, 1)">5433</span></pre>
</div>
<p><strong>修改linux系统的postgres用户的密码(密码与数据库用户postgres的密码相同)</strong></p>
<p><strong>步骤一:删除用户postgres的密码</strong></p>
<div class="cnblogs_code">
<pre>sudopasswd -d postgre</pre>
</div>
<p>步骤二:设置用户postgres的密码</p>
<div class="cnblogs_code">
<pre>sudo -u postgres passwd<br>按照提示输入新密码</pre>
</div>
<p>3,开放默认端口5432 如果安装了ufw 使用一下命令开放端口</p>
<div class="cnblogs_code">
<pre>sudo ufw allow <span style="color: rgba(128, 0, 128, 1)">5432</span></pre>
</div>
<p>4,保存文件并重新启动PostgreSQL服务以使更改生效:</p>
<div class="cnblogs_code">
<pre>sudo service postgresql restart</pre>
</div>
<p> </p>
<p><strong>PostgreSQL角色和验证方法</strong></p>
<p>PostgreSQL使用角色的概念来处理数据库访问权限,根据角色的设置方式,它可以代表一个数据库用户或一组数据库用户。</p>
<p>PostgreSQL支持多种身份验证方法,最常用的方法是:</p>
<p>1、Trust-只要满足pg_hba.conf中定义的条件,角色就可以不使用密码进行连接。</p>
<p>2、Password-角色可以通过提供密码进行连接,密码可以存储为scram-sha-256 md5和密码(明文)。</p>
<p>3、Ident-仅支持TCP/IP连接,它通过获得客户端的操作系统用户名以及可选的用户名映射来工作。</p>
<p>4、Peer-与Ident相同,但仅在本地连接上受支持。</p>
<p>PostgreSQL客户端身份验证在名为pg_hba.conf的配置文件中定义,对于本地连接,PostgreSQL设置为使用对等身份验证方法。</p>
<p>在安装PostgreSQL时自动创建“postgres”用户,该用户是PostgreSQL实例的超级用户,它等效于MySQL根用户。</p>
<p>要以“postgres”身份登录到PostgreSQL服务器,请切换到用户postgres并使用psql实用程序访问PostgreSQL提示符:</p>
<div class="cnblogs_code">
<pre>sudo su -<span style="color: rgba(0, 0, 0, 1)"> postgres
psql</span></pre>
</div>
<p> </p>
<p>在这里,你可以与PostgreSQL服务器进行交互,要退出PostgreSQL shell,请输入:</p>
<div class="cnblogs_code">
<pre>\q</pre>
</div>
<p>你可以使用sudo命令访问PostgreSQL提示符,而无需切换用户:</p>
<div class="cnblogs_code">
<pre>sudo -u postgres psql</pre>
</div>
<p>通常仅从本地主机使用postgres用户。</p>
<p><strong>创建PostgreSQL角色和数据库</strong></p>
<p>createuser命令允许你从命令行创建新角色,只有具有CREATEROLE特权的超级用户和角色才能创建新角色。</p>
<p>在以下示例中,我们将创建一个名为kylo的新角色,一个名为kylodb的数据库,并授予该角色对该数据库的特权。</p>
<p>1、首先,通过以下命令来创建角色:</p>
<div class="cnblogs_code">
<pre>sudo su - postgres -c <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">createuser kylo</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>2、接下来,使用createdb命令创建数据库:</p>
<div class="cnblogs_code">
<pre>sudo su - postgres -c <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">createdb kylodb</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>3、要向数据库上的用户授予权限,请连接到PostgreSQL shell:</p>
<div class="cnblogs_code">
<pre>sudo -u postgres psql</pre>
</div>
<p>4、运行以下查询:</p>
<div class="cnblogs_code">
<pre>grant all privileges on database kylodb to kylo;</pre>
</div>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
记忆力下降,日常日志<br><br>
来源:https://www.cnblogs.com/yushuo/p/13936498.html
頁:
[1]