Copyright 2025 HNCloud Limited.
                    香港联合通讯国际有限公司
                
            
            Ubuntu安装PostgreSQL数据库
        
        
            时间 : 2022-09-28 09:15:54
            编辑 : 华纳云
                        阅读量 : 5763
        
                
        PostgreSQL是一款功能强大的开源对象关系型数据库系统,源码开放,由社区共同维护,不受任何公司和个人控制,可以免费使用。
实验环境
操作系统:Ubuntu 20.04
以root或具有sudo权限的用户身份执行安装
在Ubuntu上安装PostgreSQL
在Ubuntu上运行以下命令来安装PostgreSQL服务器。

root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo apt update</span>
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo apt install postgresql postgresql-contrib</span>安装完成后,PostgreSQL服务会自动启动,使用psql工具连接到PostgreSQL数据库服务器并打印出其版本来验证安装情况。
查看PostgreSQL服务状态
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># systemctl status postgresql</span>
查看PostgreSQL版本号
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo -u postgres psql -c "SELECT version();"</span>
PostgreSQL的角色和认证方法
下面是PostgreSQL与shell交互操作:
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo su - postgres</span>
postgres@linuxcool:~$ psql
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type <span style="color: #d69d85;line-height: 26px">"help"</span> <span style="color: #569cd6;line-height: 26px">for</span> <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"># \q</span>
执行简单SQL语句
使用管理员权限sudo命令与shell交互执行SQL语句,如果普通用户没有权限,需要切换到root用户操作,也可以不切换用户,每次执行命令提示输入密码。
切换用户
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># su root</span>sql创建DB用户
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo su - postgres -c "createuser linuxprobe"</span>创建数据库
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo su - postgres -c "createdb linuxprobe"</span>把linuxprobe数据库授权给linuxprobe用户
root@linuxcool:~<span style="color: #57a64a;font-style: italic;line-height: 26px"># sudo -u postgres psql</span>
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type <span style="color: #d69d85;line-height: 26px">"help"</span> <span style="color: #569cd6;line-height: 26px">for</span> <span style="color: #4ec9b0;line-height: 26px">help</span>.
postgres=<span style="color: #57a64a;font-style: italic;line-height: 26px"># grant all privileges on database linuxprobe to linuxprobe;</span>
GRANT
 
                     中国香港服务器
                            中国香港服务器
                         日本服务器
日本服务器
                         美国服务器
美国服务器
                         新加坡服务器
新加坡服务器
                         
                     推荐文章
                推荐文章
             
                         
                             
                 
        