便宜VPS主机精选
提供服务器主机评测信息

Ubuntu安装PostgreSQL数据库

PostgreSQL是一款功能强大的开源对象关系型数据库系统,源码开放,由社区共同维护,不受任何公司和个人控制,可以免费使用。

实验环境

操作系统:Ubuntu 20.04

以root或具有sudo权限的用户身份执行安装

在Ubuntu上安装PostgreSQL

在Ubuntu上运行以下命令来安装PostgreSQL服务器。

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;apt&nbsp;update</span>
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;apt&nbsp;install&nbsp;postgresql&nbsp;postgresql-contrib</span>

安装完成后,PostgreSQL服务会自动启动,使用psql工具连接到PostgreSQL数据库服务器并打印出其版本来验证安装情况。

查看PostgreSQL服务状态

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;systemctl&nbsp;status&nbsp;postgresql</span>

查看PostgreSQL版本号

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;-u&nbsp;postgres&nbsp;psql&nbsp;-c&nbsp;"SELECT&nbsp;version();"</span>

PostgreSQL的角色和认证方法

下面是PostgreSQL与shell交互操作:

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;su&nbsp;-&nbsp;postgres</span>
postgres@linuxcool:~$&nbsp;psql
psql&nbsp;(10.12&nbsp;(Ubuntu&nbsp;10.12-0ubuntu0.18.04.1))
Type&nbsp;<span style="color: #d69d85;line-height: 26px">"help"</span>&nbsp;<span style="color: #569cd6;line-height: 26px">for</span>&nbsp;<span style="color: #4ec9b0;line-height: 26px">help</span>.
postgres=<span style="color: #57a64a;font-style: italic;line-height: 26px">#</span>

退出PostgreSQL终端:

postgres=<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;\q</span>

执行简单SQL语句

使用管理员权限sudo命令与shell交互执行SQL语句,如果普通用户没有权限,需要切换到root用户操作,也可以不切换用户,每次执行命令提示输入密码。

切换用户

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;su&nbsp;root</span>

sql创建DB用户

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;su&nbsp;-&nbsp;postgres&nbsp;-c&nbsp;"createuser&nbsp;linuxprobe"</span>

创建数据库

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;su&nbsp;-&nbsp;postgres&nbsp;-c&nbsp;"createdb&nbsp;linuxprobe"</span>

把linuxprobe数据库授权给linuxprobe用户

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;sudo&nbsp;-u&nbsp;postgres&nbsp;psql</span>
psql&nbsp;(10.12&nbsp;(Ubuntu&nbsp;10.12-0ubuntu0.18.04.1))
Type&nbsp;<span style="color: #d69d85;line-height: 26px">"help"</span>&nbsp;<span style="color: #569cd6;line-height: 26px">for</span>&nbsp;<span style="color: #4ec9b0;line-height: 26px">help</span>.
postgres=<span style="color: #57a64a;font-style: italic;line-height: 26px">#&nbsp;grant&nbsp;all&nbsp;privileges&nbsp;on&nbsp;database&nbsp;linuxprobe&nbsp;to&nbsp;linuxprobe;</span>
GRANT

 

未经允许不得转载:便宜VPS测评 » Ubuntu安装PostgreSQL数据库