Centos7下安装PostgreSQL14及其基本命令使用

MySQL关系型数据库目前算是互联网公司使用最多的。前两天看到一个推文,相对比国内使用MySQL,PostgreSQL在国内的普及貌似不高?国外像网络电话公司Skype公司等在大量使用PostgreSQL

作为互联网从业者,保持学习是必须的。开始学习PostgreSQL作为技术储备

PostgreSQL 二进制安装

Centos下二进制安装一般是借助于 YUM 来安装。在 PostgreSQL的官网选定版本和操作系统之后,会自动生成YUM安装脚本

对应的页面地址为 https://www.postgresql.org/download/linux/redhat/

选择对应的 PostgreSQL版本、操作系统和架构,下面就生成了 YUM安装命令

Centos7下安装PostgreSQL14及其基本命令使用
image-20221210181631953

安装命令

# 先安装PostgreSQL的YUM源
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# 安装server
sudo yum install -y postgresql14-server

# 先必须进行初始化
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
# 然后可以选择性的开启 开机自启动
sudo systemctl enable postgresql-14
# 启动服务
sudo systemctl start postgresql-14

但是有个问题

PostgreSQL 初始化数据库之后,默认的数据目录是在/var/lib/pgsql ,但是该目录是在 根分区下,一般建议放到独立的数据目录下

所以这里进行如下操作

# 新增 数据磁盘目录
mkdir /data/databases

# 停止数据库
systemctl stop postgresql-14

# 移动原始数据目录 
mv /var/lib/pgsql /data/databases/

# 创建软连
cd /var/lib && ln -/data/databases/pgsql pgsql

# 然后启动服务
systemctl start postgresql-14

最后验证安装是否成功

# 切换到 postgres 用户(yum安装时自动生成)
[root@test-demo-01-vm]$ su - postgres
# 直接输入 psql 回车, 输出 psql (14.6) 就代表安装成功
[postgres@test-demo-01-vm]$ psql
psql (14.6)
Type "help" for help.

# l 列举目前实例中的所有数据库,类似mysql中的 show databases ;
postgres=# l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres=# du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# dg
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

PostgreSQL 说明

1、从 l 列举出来的数据库清单中,看到有三个数据库

  • • PostgreSQL中默认有两个特殊的数据库,template0/template1 模板数据库就是说可以在这两个数据库中新增一些表、视图等,然后创建数据库的时候可以指定从那个模板数据库来克隆,这样新增的数据库就会有对应的模板数据库包括的表或者视图等

    • • 创建数据库不指定模板数据库的时候,默认从 template1 克隆

    • • template0 是默认最简化的数据库,它不能连接到该库,也不能对其就行修改,要保持它的"干净"

    • • 使用 template1 模板库建库时不可指定新的 encoding 和 locale,而 template0 可以

    • • 默认这两个模板数据库都不可以被删除

  • • postgres 库是默认创建的非模板数据库,它是属于 postgres 数据库用户, 在Centos服务器还有个 postgres Linux用户1、所以这里注意这三个 postgres 的区别2、Linux下切换到postgres用户直接使用客户端工具psql 连接时不加参数登录,其实就是使用 postgres 数据库用户登录3、 postgres 数据库用户是默认的超级用户

2、关于 PostgreSQL中的用户和角色

在PostgreSQL 中使用角色 来管理权限,可以把一系列权限分配给角色,当然也可以把权限分配给用户。

所以从这个角度理解的话,Postgresql 用户和角色 是一回事,用户也是角色。 从上面的du 或者 dg 的结果就能知道

📢 PostgreSQL中的所有命令都是 开头

PostgreSQL 基本命令

1、数据库操作

列举数据库l、连接数据库 c dbname

template1=# create database colinspace ;
CREATE DATABASE
template1=# l
                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
------------+----------+----------+-------------+-------------+-----------------------
 colinspace | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
(4 rows)

template1=# c colinspace
You are now connected to database "colinspace" as user "postgres".

2、表操作

  • • 列举数据库下的表d

  • • 查询具体表结构信息d tablename

  • • 查询表的索引信息 d table_index_name

colinspace=# d
Did not find any relations.
colinspace=# create table temp_app(id int not null primary key, name varchar(32) not null) ;
CREATE TABLE
colinspace=# d
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | temp_app | table | postgres
(1 row)

colinspace=# d temp_app
                     Table "public.temp_app"
 Column |         Type          | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
 id     | integer               |           | not null |
 name   | character varying(32) |           | not null |
Indexes:
    "temp_app_pkey" PRIMARY KEY, btree (id)


colinspace=# d temp_app_pkey
     Index "public.temp_app_pkey"
 Column |  Type   | Key? | Definition
--------+---------+------+------------
 id     | integer | yes  | id
primary key, btree, for table "public.temp_app"

此外

  • • 支持通配符 * 和 ? ,查询满足规则的表或者索引等

  • • d+ 详细更加详细的表信息

  • • dt 只显示匹配的表

  • • di 只显示索引

  • • ds 只显示序列

  • • dv 只显示视图

  • • df 只显示函数

  • • dn 列出所有的 schema 模式

  • • db 列出所有的表空间

  • • du 或者 dg 列出数据库中的所有角色后者用户

  • • dp 或者z 显示表的权限分配情况

3、特殊命令

3.1、timing on/off 显示和关闭 SQL 已执行的时间

colinspace=# timing on
Timing is on.
colinspace=# select * from temp_app ;
 id | name
----+------
(0 rows)

Time: 0.729 ms
colinspace=# timing off
Timing is off.

3.2、encoding utf8/gbk 等设置客户端的字符编码

3.3、pset border 0/1/2 设置输出的格式

  • • 0 表示输出的内容无边框

  • • 1 表示输出的边框只在内部 (默认行为)

  • • 2 表示内外都有边框

colinspace=# pset border 0
Border style is 0.
colinspace=# d
       List of relations
Schema   Name   Type   Owner
------ -------- ----- --------
public temp_app table postgres
(1 row)

colinspace=# pset border 1
Border style is 1.
colinspace=# d
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | temp_app | table | postgres
(1 row)

colinspace=# pset border 2
Border style is 2.
colinspace=# d
           List of relations
+--------+----------+-------+----------+
| Schema |   Name   | Type  |  Owner   |
+--------+----------+-------+----------+
| public | temp_app | table | postgres |
+--------+----------+-------+----------+
(1 row)

3.4、x 类似MySQL的在命令之后添加G

3.5、i filename 执行存储在外部文件中的sql文件或者命令, 参数是只要文件名,不带后缀

当然想要查看更多的命令及其用法,可以使用?

如果有在学PostgreSQL的,可以一起交流学习~


原文连接 Centos7下安装PostgreSQL14及其基本命令使用[1]

引用链接

[1] Centos7下安装PostgreSQL14及其基本命令使用: http://www.colinspace.com/blog/post/32/


原文始发于微信公众号(菩提老鹰):Centos7下安装PostgreSQL14及其基本命令使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/253770.html

(0)
青莲明月的头像青莲明月

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!