赞
踩
前言
上篇了解了 PostgrerSQL 数据库的部署PostgreSQL关系型数据库介绍与部署-CSDN博客,本篇将继续就其基本操作、备份与还原内容做相关介绍。
目录
- 格式:
- psql -h 服务器 -U 用户名 -d 数据库 -p 端口地址
- psql -h 127.0.0.1 -U dbuser -d exampledb -p 5432
-
- $ psql
- # 连接数据库,默认用户和数据库都是postgres
- # 相当于系统用户postgres以同名数据库用户的身份,登录数据库,这是不用输入密码的。如果一切正常,系统提示符会变为"postgres=#”,表示这时己经进入了数据库控制台。
-
- [root@localhost ~]# su - postgres # 切换 postgres 用户
- -bash-4.2$ psql # 登录 PostgreSQL 控制台,可以输入该数据库支持的一些命令
- psql (14.11)
- 输入 "help" 来获取帮助信息.
-
- postgres=# alter user postgres with password '123123'; # 修改 postgres 用户密码
- ALTER ROLE
- postgres=# \q # 退出该数据库
- -bash-4.2$

方法一:在默认配置下,本机访问 PostgreSQL
- [root@localhost ~]# su - postgres # 切换 postgres 用户
- -bash-4.2$ psql # 登录 PostgreSQL 控制台
- psql (14.11)
- 输入 "help" 来获取帮助信息.
-
- postgres=# # 此时说明用 postgres 身份登录数据库,且不需要密码
- postgres=# \password postgres # 给 postgres 用户设置密码
- Enter new password for user "postgres":
- 再输入一遍:
方法二:创建新用户来访问 PostgreSQL
- [root@localhost ~]# su - postgres # 切换linux用户postgres
- -bash-4.2$ psql # 执行psql
- psql (14.11)
- 输入 "help" 来获取帮助信息.
-
- postgres=# create user dbfql with password '123456'; # 创建数据库用户dbfql
- CREATE ROLE
- postgres=# craete database db1 owner dbfql; # 创建数据库db1
- CREATE DATABASE
- postgres=# grant all privileges on database db1 to dbfql; # 将db1数据库的所有权都赋予dbdql
- GRANT
- postgres=# \q-bash-4.2$ exit
- 登出
-
- [root@localhost ~]# useradd dbfql # 创建普通用户dbfql
- [root@localhost ~]# passwd dbfql
- [root@localhost ~]# su - dbfql
- [dbfql@localhost ~]$ psql -d db1
- psql (14.11)
- 输入 "help" 来获取帮助信息.
-
- db1=>

方法三:已存在普通用户,在数据库创建同名用户和数据库
- [root@localhost ~]# id lisi
- uid=1002(lisi) gid=1002(lisi) 组=1002(lisi)
- [root@localhost ~]# su - postgres
- -bash-4.2$ psql
- psql (14.11)
- 输入 "help" 来获取帮助信息
- postgres=# create user lisi with password '123456';
- CREATE ROLE
- postgres=# create database lisidb owner lisi;
- CREATE DATABASE
- postgres=# grant all privileges on database lisidb to lisi;
- GRANT
- postgres=# \q
- -bash-4.2$ exit
- 登出
-
- [root@localhost ~]# su - lisi
- [lisi@localhost ~]$ psql -d lisidb
- psql (14.11)
- 输入 "help" 来获取帮助信息.
-
- lisidb=>

以上篇编译安装目录为例,默认安装配置文件路径如下:
- 允许TCP端口5432的传入连接,并将规则永久性地添加到防火墙配置中,防火墙关闭可以忽略
- firewall-cmd --add-port=5432/tcp --permanent # --permanent选项表示该规则是永久性的
- firewall-cmd --reload # 重新加载防火墙配置
- [root@localhost ~]# vim /pgsql/data/postgresql.conf # 主要配置文件
- listen_addresses = '*'
- # 在所有IP地址上监听,从而允许远程连接到数据库服务器,默认只允许本机登录
-
- [root@localhost ~]# vim /pgsql/data/pg_hba.conf # "主机基于身份验证" 配置文件
- # "local" is for Unix domain socket connections only
- local all all peer
- # IPv4 local connections:
- host all all 127.0.0.1/32 scram-sha-256
- host all all 192.168.190.0/24 trust # 添加此行
- # 允许任意用户(第二个 "all")从192.168.190段地址上以md5密码方式访问任意数据库(第一个 "all")
- # peer: 使用 "peer" 认证方式时,系统会检查操作系统用户是否与要连接到数据库的数据库角色名称相匹配;不需要输入密码来连接到数据库,只需确保操作系统用户名和数据库角色名称匹配即可完成认证。
- # md5:当使用 "md5" 时,客户端连接时需要提供经过 MD5 加密的密码进行认证。
- # trust方法表示无需密码即可连接数据库
- # scram-sha-256:相比之下,"scram-sha-256" 使用了更安全的加密机制,比 MD5 更安全。
- [root@localhost ~]# su - postgres
- 上一次登录:日 3月 24 15:17:53 CST 2024pts/1 上
- [postgres@localhost ~]$ psql
- psql (14.1)
- Type "help" for help.
-
- postgres=# alter user postgres with password '123123'; # 登录数据修改密码
- ALTER ROLE
- 或者:
- postgres=# \password postgres # 给 postgres 用户设置密码
- Enter new password for user "postgres":
- 再输入一遍:
- [root@localhost ~]# systemctl restart postgresql.service
- 或者:
- [postgres@localhost ~]$ pg_ctl restart # 重启
① 使用 linux 系统字符登录
- [root@localhost ~]# psql -U postgres -h 192.168.190.100
- 用户 postgres 的口令:
- psql (14.11, 服务器 14.1)
- 输入 "help" 来获取帮助信息.
-
- postgres=#
② 使用 Navicat 工具登录(可能存在版本问题,可以修改 pg_hba.conf 认证方式为 trust)
- 在数据库控制台下创建操作:
- postgres=# create database mydb;
- CREATE DATABASE
- 也可以在壳下进行创建操作:
- [postgres@localhost ~]$ createdb 库名;
- mydb=# \l
- 数据库列表
- 名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
- -----------+----------+----------+-------------+-------------+-----------------------
- mydb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
- template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
- | | | | | postgres=CTc/postgres
- template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
- | | | | | postgres=CTc/postgres
- postgres=# \c mydb;
- 您现在已经连接到数据库 "mydb",用户 "postgres".
- postgres=# drop database mydb; # 注意,删除数据库是一个不可逆的操作
- ERROR: database "mydb" is being accessed by other users
- DETAIL: There is 1 other session using the database.
- # 在PostgreSQL中,如果有其他用户正在使用数据库,你就不能删除它
-
- postgres=# select * from pg_stat_activity where datname = 'mydb';
- # 可以通过查询 pg_stat_activity 视图来检查是否有活动的会话,确保没有用户正在使用数据库 "mydb",再次删除
- 语法与mysql基本相似:
- postgres=# \c mydb # 切换到 mydb 库
- You are now connected to database "mydb" as user "postgres".
- mydb=# create table class (id int,name char(15)); # 新建表 class
- CREATE TABLE
- # 在 mysql 中 auto_increment 代表自增,在这里使用 serial 可以实现自增,但是不能与 int 一起使用
- mydb=# insert into class values (1,'zhangsan');
- mydb=# insert into class values (2,'lisi');
- mydb=# \dt # 列出当前数据库中的所有表
- List of relations
- Schema | Name | Type | Owner
- --------+--------+-------+----------
- public | class | table | postgres
-
-
- mydb=# \d class # 查看表结构,不指定表可以查看当前库所有表,\d相当于desc
- Table "public.class"
- Column | Type | Collation | Nullable | Default
- --------+---------------+-----------+----------+---------
- id | integer | | |
- name | character(15) | | |
-
- mydb=# select * from class; # 查看表内容
- id | name
- ----+-----------------
- 1 | zhangsan
- 2 | lisi

- mydb=# select * from class;
- id | name
- ----+-----------------
- 2 | lisi
- 1 | wangwu
- (2 rows)
-
- mydb=# select * from class order by id; # 按 id 升序查看
- id | name
- ----+-----------------
- 1 | wangwu
- 2 | lisi
- mydb=# delete from class where id=1;
- mydb=# select * from class;
- id | name
- ----+-----------------
- 2 | lisi
PostgreSQL 使用环境变量 PGDATA 指向的目录做为数据存放的目录。这个目录是在安装时指定的,所以在安装时需要指定一个合适的目录作为数据目录的根目录,而且,每一个 PG 数据库实例都需要有这样的一个目录。此数据目录的初始化是使用命令 initdb 来完成的。初始化完成后, PGDATA 数据目录下就会生成三个配置文件
在一个 PostgreSQL 数据库系统中,数据的组织结构可以分为以下五层:
PostgreSQL 有一些术语与其他数据库中不一样,了解了这些术语的意思,就能更好地看懂PostgreSQL 中的文档。与其他数据库不同的术语如下:
template1 和 template0 是 PostgreSQL 的模板数据库。所谓模板数据库就是创建新 database 时,PostgreSQL 会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件。
PostgreSQL安装好以后会默认附带两个模板数据库: 默认模板库为 template0 和template1。默认模板库为 template1,也可以指定template0
template1 和t emplate0 的区别主要有两点:
注意:template0和template1都不能被删除。
在 PostgreSQL 中,Schema 是一种用于组织和命名数据库对象(如表、视图、函数等)的机制。它提供了命名空间隔离、权限控制和代码组织的功能,使得在同一个数据库中可以创建多个相互独立的对象,更精细地控制用户对数据库对象的访问,以及更好地组织和管理数据库对象。每个数据库都包含一个默认的 Schema,名为 public,但是你也可以创建自己的 Schema。
使用模式的优势:
- mydb=# create schema myschema;
- CREATE SCHEMA
- mydb=# \dn # 查看模式
- mydb=# create table myschema.student (id serial,name char(15));
- CREATE TABLE
- mydb=# \dt myschema.* # 查看 Schema 中的所有表
- List of relations
- Schema | Name | Type | Owner
- ----------+---------+-------+----------
- myschema | student | table | postgres
-
- mydb=# insert into myschema.student values (1,'lisi');
- INSERT 0 1
- mydb=# select * from myschema.student;
- id | name
- ----+-----------------
- 1 | lisi
- 格式:
- drop schema schema_name;
-
- 示例:
- postgres=# \c mydb1
- mydb1=# create schema schema_db1;
- mydb1=# \dn
- List of schemas
- Name | Owner
- ------------+----------
- myschema | postgres
- public | postgres
- schema_db1 | postgres
- mydb1=# drop schema schema_db1; # 删除模式
- mydb1=# \dn
- List of schemas
- Name | Owner
- ----------+----------
- myschema | postgres
- public | postgres

新建用户、修改密码等操作,前文已做相关介绍,这里继续介绍一下用户的权限管理与用户删除。
- mydb=# create user fql with password '123456'; # 创建用户 fql 并设置密码 123456
- CREATE ROLE
- mydb=# alter user fql with password '666666'; # 修改用户 fql 密码为 666666
- ALTER ROLE
- 数据库授权,赋予指定账户指定数据库所有权限,格式如下:
- grant all privileges on database 'dbname' To 'username';
-
- mydb=# grant all privileges on database mydb to fql;
- # 将数据库 mydb 赋予用户 fql 所有权限
- mydb=# grant all privileges on all tables in schema public to fql;
- # 但此时用户还是没有读写权限,需要继续授权表,授予用户 fql 对 public Schema 中的所有表的所有权限
- # 注意,该sql语句必须在所要操作的数据库里执行
-
- 查看权限:
- [root@localhost ~]# psql -U fql -d mydb;
- \du # 列出所有的用户,包括他们的角色、登录权限、超用户权限等信息
- \du username # 查看指定用户的权限
-
- 移除指定账户指定数据库/表所有权限:
- mydb=# revoke all privileges on database mydb from fql;
- mydb=# revoke all privileges on all tables in schema public from fql;

- mydb=# drop user 用户名;
- # 注意,需要删除给予库/表的权限,才可以执行删除动作
PostgreSQL 提供了 pg_dump 实用程序来简化备份单个数据库的过程。 必须以对要备份的数据库具有读取权限的用户身份运行此命令。以 postgres 用户身份登录:
- [root@localhost ~]# su - postgres
- [postgres@localhost ~]$
① 通过运行以下命令将数据库的内容转储到文件中
- [postgres@localhost ~]$ pg_dump mydb > /pgsql/mydb.bak
-
- [root@localhost pgsql]# ll
- 总用量 8
- drwx------. 19 postgres postgres 4096 4月 6 15:06 data
- -rw-rw-r--. 1 postgres postgres 3500 4月 6 18:30 mydb.bak
- # 生成的备份文件 mydb.bak 可以使用 scp 传输到另一台主机,也可以存储在本地以供以后使用
② 模拟删除数据库,并新建空库
- [root@localhost pgsql]# psql
- postgres=# drop database mydb;
-
- 恢复数据库,需要先建立一个空库:
- postgres=# create database mydb1;
③ 恢复,验证数据
- [root@localhost pgsql]# psql mydb1 < /pgsql/mydb.bak;
- postgres=# \c mydb1
- You are now connected to database "mydb1" as user "postgres".
- mydb1=# \dt
- List of relations
- Schema | Name | Type | Owner
- --------+--------+-------+----------
- public | class | table | postgres
- public | class1 | table | postgres
备份格式有几种选择:
注意:默认情况下,PostgresQL 将忽路备份过程中发生的任何错误,这可能导致备份不完整,要防止这种情况,可以使用 -1 选项运行 pg_dunp 命令。这会将整个备份过程视为单个事务,这将在发生错误时阻止部分备份。
由于 pg_dump 一次只创建一个数据库的备份,因此它不会存储有关数据库角色或其他群集范围配置的信息。 要存储此信息并同时务份所有数据库,可以使用 pg_dumpall。
① 创建备份文件
[postgres@localhost ~]$ pg_dumpall > /pgsql/allpg.bak
② 从备份还原所有数据库
- [postgres@localhost ~]$ psql -f /pgsql/allpg.bak postgres
- # -f 选项用于指定要执行的 SQL 脚本文件或文件路径
- 备份数据库:
- pg_dump -U postgres -f /pgsql/postgres.sgl postgres
- # 导出 postgres 数据岸保存为 postgres.sql
- pg_dump -U postgres -f /pgsql/postgres.sql -t test postgres
- # 导出 postgres 数据库中表 test 的数据
- pg_dump -U postgres -F t -f /pgsql/postgres.tar postgres
- # -F t:这个选项指定了备份文件的格式。t 表示 "tar" 格式,导出 postgres 数据库以 tar 形式压缩保存为 postgres.tar
-
- 恢复数据库:
- psgl-postgres -f /pgsql/postgres.sql newsql
- # 恢复 postgres.sql 数据到 newsql 数据库
- pg_restore --
- # 从 pg_dump 创建的备份文件中恢复 PostgresQL 数据库,用于恢复由 pg_dump 转储的任何非纯文本格式中的PostgreSQL 数据库
- pg_restore -U postgres -d newsql /pgsql/postgres.tar
- # 恢复 postgres.tar 数据列 newsql 数据库
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。