Linux——PostgreSQL数据库日常维护
目录
一,PG数据库的基本使用
1,基本使用
2,登陆数据库
3,数据库的操作
4,数据表的操作
2,模式操作命令
1,如何在指定模式中创建表
2,postgresql的模式隔离性
三,数据操作
三,pgsql的备份与恢复
1,sql转储
2,从转储中恢复
四,远程连接pgsql
一,PG数据库的基本使用
1,基本使用
在Postgresql的交互式终端psql中,"\" 开头的命令称为原命令,用于快速管理数据库。
命令 | 作用 |
\l | 列出所有数据库 |
\c [数据库名] | 切换数据库 |
\dn | 列出所有数据库(scheam) |
\db | 列出所有表空间 |
|? | 显示 pgsql 命令的说明 |
\q | 退出 psql |
\dt | 列出当前数据库的所有表 |
\d [table] | 查看表结构 |
\du | 列出所有用户 |
2,登陆数据库
su - postgres ##使用postgressl数据库一定要切换用户
pg_ctl -D /usr/local/pgsql/data -f logfile start ##启动数据库
psql ##进入到postgresql数据库postgres=# ##=前为登陆某个数据库的库名
3,数据库的操作
postgres=# \l ##查看所有的数据库List of databasesName | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc postgres=# \l+ ##加上+可以看到数据的大小List of databasesName | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges | Size | Tablespace | D
escription
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------+---------+------------+-----------------
---------------------------postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | | 7229 kB | pg_default | default administ##创建库
postgres=# create database mydb;
CREATE DATABASE
##切换数据库
postgres=# \c mydb;
You are now connected to database "mydb" as user "postgres".
##删除库
postgres=# drop database mydb;
DROP DATABASE##查看数据库的大小
postgres=# select pg_database_size('mydb');pg_database_size
------------------7324163
(1 row)
##显示单位查看
postgres=# select pg_size_pretty(pg_database_size('mydb'));pg_size_pretty
----------------7153 kB
(1 row)
4,数据表的操作
##创建表
mydb=# create table test(id int);
CREATE TABLE
##查看表的信息
mydb=# \dt;List of relationsSchema | Name | Type | Owner
--------+------+-------+----------public | test | table | postgres
(1 row)
复制表结构和数据,语法如下:
create table 新表名 AS table 旧表名
mydb=# create table test1 as table test; ##将test复制为test1
SELECT 0
mydb=# \dt;List of relationsSchema | Name | Type | Owner
--------+-------+-------+----------public | test | table | postgrespublic | test1 | table | postgres
(2 rows)##删除表
mydb=# drop table test1;
DROP TABLE
mydb=# \dt;List of relationsSchema | Name | Type | Owner
--------+------+-------+----------public | test | table | postgres
(1 row)##查看表的结构
mydb=# \d test;Table "public.test"Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------id | integer | | |
2,模式操作命令
在 PostgreSQL 中,模式(Schema)是一个逻辑容器,用于组织和管理数据库对象(如表、视图、函数、索引等)它类似于文件系统中的文件夹,帮助你在同一个数据库中分类存储不同的对象,避免命名冲突,并实现权限隔离。创建表时的顺序 库——模式——表 以下是PostgreSQL 中常见的模式及其用途:
- 公共模式(public)
这是创建新数据库时自动生成的默认模式。要是你在创建数据库对象时没有特别指定模式,那么这些对象就会被存放在public
模式中。
CREATE TABLE users(表名) (id SERIAL, name TEXT); ##会在public模式下创建表
- 信息模式(information_schema)
这是一个元数据模式,它存储着有关数据库结构的描述信息。借助查询information_schema
,你能够获取数据库、表、列等对象的详细信息。
select scheam_name from information_schema.schemata; ##列出当前库中所有模式
-
pg_catalog
该模式包含 PostgreSQL 的系统表和内置函数,像pg_class
(存储表信息)、pg_attribute
(存储列信息)等都在其中。通常情况下,用户不会直接和这个模式打交道。
- 用户自定义模式
你可以根据自身需求创建模式,以此将数据库对象进行逻辑分组。
create scheam aaa; ##创建一个名为aaa的模式
create table aaa.bbb (id serial); ##在该模式中创建一个名为bbb的表
模式的主要用途
- 避免命令冲突:当多个用户或者应用使用同一个数据库时,模式可以防止对象名称重复。
- 简化权限管理:能够对整个模式统一设置权限,比如授予或撤销对模式中所有对象的访问权限。
- 组织大型数据库:可以依据业务逻辑对数据库对象进行分组,例如将表、视图和函数分别存放在不同的模式中。
##在postgres库中创建名为hr的模式
postgres=# create schema hr;
CREATE SCHEMA##当我们创建表,视图时不指定模式,默认和放在public模式中
postgres=# show search_path;search_path
-----------------"$user", public
(1 row)##删除模式
postgres=# drop schema hr;
DROP SCHEMA
##强制删除模式及其所有的对象
postgres=# drop schema hr cascade;
DROP SCHEMA##列出当前库中的所有模式
postgres=# select schema_name from information_schema.schemata;schema_name
--------------------publichrinformation_schemapg_catalogpg_toast
(5 rows)
1,如何在指定模式中创建表
未指定模式时创建的对象(表,视图等)会按 search path 顺序创建到第一个可用的模式中
##在hr模式中创建名为abc的表
postgres=# create table hr.abc(id int);
##查看当前模式
postgres=# select current_schema();current_schema
----------------public
(1 row)##切换到hr模式
postgres=# set search_path to hr;
SET
postgres=# select current_schema();current_schema
----------------hr
(1 row)##查看刚刚创建名为abc的表
postgres=# \dtList of relationsSchema | Name | Type | Owner
--------+------+-------+----------hr | abc | table | postgres
(1 row)##查看表时的搜索路径
postgres=# show search_path;search_path
-------------hr
(1 row)
2,postgresql的模式隔离性
PostgreSQl 的模式是数据库内的逻辑分组,不同模式可以存在同名表。这也是和 mysql 的不同之处,跨模式査询需显式指定模式名(如 schema1.users),或通过 search path 设置默认模式。
- 创建一个数据库
postgres=# create database mydb;
##切换到mydb数据库 postgres=# \c mydb
-
在数据库中创建两个模式
mydb=# create schema hr1;
mydb=# create schema hr2;
-
在每个模式中创建同名表,并插入数据
##在hr1中创建aaa表,并加入数据
mydb=# create table hr1.aaa(id int);
mydb=# insert into hr1.aaa values(1);
##在hr2中创建bbb表,并加入数据
mydb=# create table hr2.bbb(id int);
mydb=# insert into hr2.bbb values(1);
- 跨模式查询
##当前模式为public模式
mydb=# show search_path;search_path
-----------------"$user", public
(1 row)##进行跨模式查询
mydb=# select * from hr1.aaa;id
----1
(1 row)mydb=# select * from hr2.bbb;id
----1
(1 row)mydb=# set search_path to hr1;
SET
mydb=# select * from aaa;id
----1##设置默认模式为hr1
mydb=# set search_path to hr1;
SETmydb=# select * from aaa;id
----1
三,数据操作
##添加数据
postgres=# create table test(id int,name char(10),age int);
CREATE TABLE
postgres=# insert into test values(1,'lisi','22');
INSERT 0 1##查询数据
postgres=# select * from test;id | name | age
----+------------+-----1 | lisi | 22
(1 row)##修改数据
postgres=# update test set age=20 where id=1;
UPDATE 0
postgres=# select * from test;id | name | age
----+------------+-----1 | lisi | 20
(1 row)##删除数据
postgres=# delete from test where id=1;
DELETE 0postgres=# select * from test;id | name | age
----+------+-----
(0 rows)
三,pgsql的备份与恢复
PostgreSQ 数据库应当被定期地备份。虽然过程相当简单,但清晰地理解其底层技术和假设是非常重要的。
有三种不同的基本方法来备份 PostgreSQ 数据:SQL 转储 文件系统级 备份连续归档
1,sql转储
SQL 转储方法的思想是创建一个由 SQL,命令组成的文件,当把这个文件回馈给服务器时,服务器将利用其中的SQL命令重建与转储时状态一样的数据库。PostgreSQL 为此提供了工具 pg dump。这个工具的基本用法是:
pg_dump [要导出数据库的名字] > [导出后的名字]
##查看mydb数据库的内容
mydb=# select * from hr1.aaa;id
----1
(1 row)mydb=# select * from hr2.bbb;id
----1
(1 row)##做完部分后删除数据库mydb
[postgres@localhost ~]$ pg_dump mydb > ./mydbbak##切换到其他数据库
mydb=# \c postgres
You are now connected to database "postgres" as user "postgres".
##删除mydb数据库
postgres=# drop database mydb;
DROP DATABASE
2,从转储中恢复
pg_dump 生成的文本文件可以由 psq1 程序读取。 从转储中恢复的常用命令是:
psql [数据库名] < 备份的数据库路径/名字
##直接恢复会报错
[postgres@localhost ~]$ psql mydb < mydbbak
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "mydb" does not exist##需要先创建mydb数据库
create database mydb;##继续进行备份
[postgres@localhost ~]$ psql mydb < mydbbak ##查看恢复情况
postgres=# \c mydb;
You are now connected to database "mydb" as user "postgres".
mydb=# select * from hr1.aaa;id
----1
(1 row)mydb=# select * from hr2.bbb;id
----1
(1 row)
四,远程连接pgsql
默认 PostgreSQl 监听的地址是 127.0.0.1,别的机器无法远程连接上,所以需要调整,修改 postgresql.conf 文件。
/usr/local/pgsql/data/postgresql.conf ##源码编译安装的pgsql配置文件位置
vim /usr/local/pgsql/data/postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on; ##60行修改##重启服务
[postgres@localhost data]$ pg_ctl -D /usr/local/pgsql/data -l logfile restart##修改访问权限
[root@localhost data]# vim /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 trust ##添加修改当前部分91行##host:这指定了连接类型。host 表示该规则适用于通过 TCP/IP 进行的远程连接。如果是本地连接,通常会使用 loca1。##all:这定义了哪些数据库可以接受这个规则。a11 表示这个规则适用于所有数据库。你也可以指定特定的数据库名,例如mydatabase。##all:这定义了哪些用户可以接受这个规则。al1表示这个规则适用于所有用户。你也可以指定特定的用户名,例如myuser。##0.0.0.0/0:这定义了哪些客户端IP地址或 IP 地址范围可以接受这个规则。0.0.0.0/0 是一个特殊的 CIDR 表示法,它表示任何 IP 地址(即没有 IP 地址限制)。你也可以指定具体的 IP 地址,如 192.168.1.100,或者 IP 地址范围,如 192.168.1.0/24。##trust::这定义了认证方法。trust 表示不需要密码或其他任何形式的认证,客户端可以直接连接。这通常只在本地或受信任的网络环境中使用,如果修改密码,使用更安全的认证方法,如md5或password(对于较新版本的 PostgreSQl,建议使用 scram-sha-256)。[postgres@localhost data]$ pg_ctl -D /usr/local/pgsql/data -l logfile restart##记得关闭防火墙
1,验证
##客户端验证
[root@localhost ~]# useradd postgres
[root@localhost ~]# su - postgres[postgres@localhost ~]$ psql -h 192.168.10.102
postgres=# ##如果远程登陆密码验证
host all all 0.0.0.0/0 trust
host all all 0.0.0.0/0 md5 ##改为md5##修改密码为123456
postgres=# alter user postgres with password '123456';##别忘了重启postgresql[postgres@localhost ~]$ psql -h 192.168.10.102
用户 postgres 的口令:
postgres=#