当前位置: 首页 > news >正文

PostgreSQL数据库笔记

PostgreSQL 是什么

PostgreSQL(简称Postgres或PG)是一个功能强大、可靠性高、可扩展性好的开源对象-关系数据库服务器(ORDBMS),它以加州大学伯克利分校计算机系开发的POSTGRES版本4.2为基础。
发展历程

起源与发展:PostgreSQL是伯克利的POSTGRES软件包的继承者,并经过不断的发展和完善,现已成为世界上最先进的开源数据库系统之一。
特点

许可证的灵活性:由于许可证的灵活,任何人都可以以任何目的免费使用、修改和分发PostgreSQL,无论是私用、商用还是学术研究使用。

对象-关系型数据库:PostgreSQL支持大部分SQL标准,并提供了许多其他现代特性,如复杂查询、外键、触发器、视图、事务完整性以及多版本并发控制(MVCC)等。
主要功能与特性

可扩展性:PostgreSQL可以通过增加新的数据类型、函数、操作符、聚集函数和索引方法等方式进行扩展。

数据类型丰富:支持包括文本、任意精度的数值数组、JSON数据、枚举类型、XML数据等在内的多种数据类型。

复杂查询与优化:支持复杂的查询操作,包括连接、子查询、聚合函数、窗口函数等,并配备了高效的查询规划器/优化器。

全文检索与NoSQL支持:通过Tsearch2或OpenFTS支持全文检索,并原生支持JSON、JSONB、XML、HStore等NoSQL数据类型。

数据仓库与ETL:能平滑迁移至同属PostgreSQL生态的GreenPlum、DeepGreen、HAWK等数据仓库,并使用FDW进行ETL(提取、转换、加载)。

安全性:提供了强大的安全性措施,包括用户身份验证、数据加密和访问控制等,确保数据的安全性和保密性。
应用场景

Web应用程序:如电子商务网站、社交媒体平台等。

科学和研究项目:用于存储和分析大规模的实验数据和研究结果。

地理信息系统(GIS):用于存储和处理地理空间数据。

大数据分析:用于存储和分析大规模数据集。

企业级应用程序:在各种企业级应用中作为可靠的数据库管理系统。
官网

https://www.postgresql.org/
安装

在Linux上,可以使用RPM源安装或从源代码编译安装。
添加 PostgreSQL 12 软件包存储库

yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

下载 PostgreSQL 12 客户端、服务端

yum -y install postgresql12-server postgresql12

PostgreSQL 数据库初始化

/usr/pgsql-12/bin/postgresql-12-setup initdb

设置开机自启

systemctl enable postgresql-12 && systemctl start postgresql-12

PostgreSQL不推荐使用root管理,在安装成功postgreSQL后,他默认会给你创建一个用户:postgres。

输入 psql 进入到postgreSQL提供的客户端。

bash-4.2$ psql --help
could not change directory to "/root": 权限不够
psql is the PostgreSQL interactive terminal.Usage:psql [OPTION]... [DBNAME [USERNAME]]General options:-c, --command=COMMAND    run only single command (SQL or internal) and exit-d, --dbname=DBNAME      database name to connect to (default: "postgres")-f, --file=FILENAME      execute commands from file, then exit-l, --list               list available databases, then exit-v, --set=, --variable=NAME=VALUEset psql variable NAME to VALUE(e.g., -v ON_ERROR_STOP=1)-V, --version            output version information, then exit-X, --no-psqlrc          do not read startup file (~/.psqlrc)-1 ("one"), --single-transactionexecute as a single transaction (if non-interactive)-?, --help[=options]     show this help, then exit--help=commands      list backslash commands, then exit--help=variables     list special variables, then exitInput and output options:-a, --echo-all           echo all input from script-b, --echo-errors        echo failed commands-e, --echo-queries       echo commands sent to server-E, --echo-hidden        display queries that internal commands generate-L, --log-file=FILENAME  send session log to file-n, --no-readline        disable enhanced command line editing (readline)-o, --output=FILENAME    send query results to file (or |pipe)-q, --quiet              run quietly (no messages, only query output)-s, --single-step        single-step mode (confirm each query)-S, --single-line        single-line mode (end of line terminates SQL command)Output format options:-A, --no-align           unaligned table output mode--csv                CSV (Comma-Separated Values) table output mode-F, --field-separator=STRINGfield separator for unaligned output (default: "|")-H, --html               HTML table output mode-P, --pset=VAR[=ARG]     set printing option VAR to ARG (see \pset command)-R, --record-separator=STRINGrecord separator for unaligned output (default: newline)-t, --tuples-only        print rows only-T, --table-attr=TEXT    set HTML table tag attributes (e.g., width, border)-x, --expanded           turn on expanded table output-z, --field-separator-zeroset field separator for unaligned output to zero byte-0, --record-separator-zeroset record separator for unaligned output to zero byteConnection options:-h, --host=HOSTNAME      database server host or socket directory (default: "local socket")-p, --port=PORT          database server port (default: "5432")-U, --username=USERNAME  database user name (default: "postgres")-w, --no-password        never prompt for password-W, --password           force password prompt (should happen automatically)For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.Report bugs to <pgsql-bugs@lists.postgresql.org>.

查看有哪些库:
su postgres

\l

在这里插入图片描述

如果是新安装的,有三个库,一个是postgres,template0,template1。

# 切换到postgres用户
[root@review ~]# su postgres
bash-4.2$ psql
could not change directory to "/root": 权限不够
psql (12.20)
Type "help" for help.postgres=# \lList of databasesName    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  -----------+----------+----------+-------------+-------------+----------------------
-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/postgrestemplate1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          
+|          |          |             |             | postgres=CTc/postgres
(3 rows)postgres=# 

配置文件

[root@review ~]# ll /var/lib/pgsql/12/data
总用量 64
drwx------. 5 postgres postgres    41 10月 25 20:53 base
-rw-------. 1 postgres postgres    30 10月 25 20:33 current_logfiles
drwx------. 2 postgres postgres  4096 10月 25 20:32 global
drwx------. 2 postgres postgres    32 10月 25 20:31 log
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_commit_ts
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_dynshmem
-rw-------. 1 postgres postgres  4517 10月 25 20:33 pg_hba.conf
-rw-------. 1 postgres postgres  1636 10月 25 20:31 pg_ident.conf
drwx------. 4 postgres postgres    68 10月 25 20:53 pg_logical
drwx------. 4 postgres postgres    36 10月 25 20:31 pg_multixact
drwx------. 2 postgres postgres    18 10月 25 20:31 pg_notify
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_replslot
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_serial
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_snapshots
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_stat
drwx------. 2 postgres postgres    63 10月 25 20:56 pg_stat_tmp
drwx------. 2 postgres postgres    18 10月 25 20:31 pg_subtrans
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_tblspc
drwx------. 2 postgres postgres     6 10月 25 20:31 pg_twophase
-rw-------. 1 postgres postgres     3 10月 25 20:31 PG_VERSION
drwx------. 3 postgres postgres    60 10月 25 20:31 pg_wal
drwx------. 2 postgres postgres    18 10月 25 20:31 pg_xact
-rw-------. 1 postgres postgres    88 10月 25 20:31 postgresql.auto.conf
-rw-------. 1 postgres postgres 26736 10月 25 20:31 postgresql.conf
-rw-------. 1 postgres postgres    58 10月 25 20:31 postmaster.opts
-rw-------. 1 postgres postgres   103 10月 25 20:31 postmaster.pid

远程连接
改用户名

alter user postgres with password ‘wkb456’;
PostgreSQL要基于配置文件修改,才能制定用户是否可以远程连接。
用户级别配置

修改 pg_hba.conf 文件

local:代表本地连接,host代表可以指定连接的ADDRESS。database:编写数据库名,如果写all,代表所有库都可以连接。user:编写连接的用户,可以写all,代表所有用户。address:代表那些IP地址可以连接。method:代表加密方式。

允许任意地址的全部用户连接所有数据库:

host    all             all             0.0.0.0/0               md5# TYPE  DATABASE        USER            ADDRESS                 METHOD# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident

服务级别配置

修改 postgresql.conf 文件

# - Connection Settings -#listen_addresses = 'localhost'         # what IP address(es) to listen on;# comma-separated list of addresses;# defaults to 'localhost'; use '*' for all# (change requires restart)
#port = 5432                            # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/var/run/postgresql, /tmp'  # comma-separated list of directories# (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation# (change requires restart)
#bonjour = off                          # advertise server via Bonjour# (change requires restart)
#bonjour_name = ''                      # defaults to the computer name# (change requires restart)

修改为

listen_addresses = '*'         # what IP address(es) to listen on;

保存后退出,然后重启 postgresql-12

systemctl restart postgresql-12

数据库日志

postgresql.conf 文件,默认情况下,只保存7天的日志,循环覆盖。

# REPORTING AND LOGGING
#------------------------------------------------------------------------------# - Where to Log -log_destination = 'stderr'              # Valid values are combinations of# stderr, csvlog, syslog, and eventlog,# depending on platform.  csvlog# requires logging_collector to be on.# This is used when logging to stderr:
logging_collector = on                  # Enable capturing of stderr and csvlog# into log files. Required to be on for# csvlogs.# (change requires restart)# These are only used if logging_collector is on:
log_directory = 'log'                   # directory where log files are written,# can be absolute or relative to PGDATA
log_filename = 'postgresql-%a.log'      # log file name pattern,# can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,# begin with 0 to use octal notation
log_truncate_on_rotation = on           # If on, an existing log file with the# same name as the new log file will be# truncated rather than appended to.# But such truncation only occurs on# time-driven rotation, not on restarts# or size-driven rotation.  Default is# off, meaning append to existing files# in all cases.
log_rotation_age = 1d                   # Automatic rotation of logfiles will# happen after that time.  0 disables.
log_rotation_size = 0                   # Automatic rotation of logfiles will# happen after that much log output.# 0 disables.

代表日志是开启状态。
logging_collector = on

日志存放的路径,默认放到当前目录下的log里。
log_directory = ‘log’

日志的文件名,默认是postgresql为前缀,星期作为后缀。
log_filename = ‘postgresql-%a.log’

默认一周过后,日志文件会被覆盖。
log_truncate_on_rotation = on

一天产生一个日志文件。
log_rotation_age = 1d

没有限制日志文件的大小。
log_rotation_size = 0

相关文章:

PostgreSQL数据库笔记

PostgreSQL 是什么 PostgreSQL&#xff08;简称Postgres或PG&#xff09;是一个功能强大、可靠性高、可扩展性好的开源对象-关系数据库服务器&#xff08;ORDBMS&#xff09;&#xff0c;它以加州大学伯克利分校计算机系开发的POSTGRES版本4.2为基础。 发展历程 起源与发展&a…...

财务软件源码SaaS云财务

在如今的商业环境中&#xff0c;准确的财务管理是一家企业取得成功的关键。然而&#xff0c;传统的财务管理方法已经无法满足现代企业的需求&#xff0c;需要一个全新的解决方案。推出了全新的财务软件为您提供完美的解决方案。 选择财务软件源码&#xff0c;您将享受到以下优…...

Elasticsearch集群和Kibana部署流程

搭建Elasticsearch集群 1. 进入Elasticsearch官网下载页面&#xff0c;下载Elasticsearch 在如下页面选择Elasticsearch版本&#xff0c;点击download按钮&#xff0c;进入下载页面 右键选择自己操作系统对应的版本&#xff0c;复制下载链接 然后通过wget命令下载Elastics…...

丹摩征文活动 | 丹摩智算:大数据治理的智慧引擎与实践探索

丹摩DAMODEL&#xff5c;让AI开发更简单&#xff01;算力租赁上丹摩&#xff01; 目录 一、引言 二、大数据治理的挑战与重要性 &#xff08;一&#xff09;数据质量问题 &#xff08;二&#xff09;数据安全威胁 &#xff08;三&#xff09;数据管理复杂性 三、丹摩智算…...

【Django】Clickjacking点击劫持攻击实现和防御措施

Clickjacking点击劫持 1、clickjacking攻击2、clickjacking攻击场景 1、clickjacking攻击 clickjacking攻击又称为点击劫持攻击&#xff0c;是一种在网页中将恶意代码等隐藏在看似无害的内容&#xff08;如按钮&#xff09;之下&#xff0c;并诱使用户点击的手段。 2、clickj…...

Ansys Zemax | 手机镜头设计 - 第 4 部分:用LS-DYNA进行冲击性能分析

该系列文章将讨论智能手机镜头模组设计的挑战&#xff0c;从概念和设计到制造和结构变形分析。本文是四部分系列中的第四部分&#xff0c;它涵盖了相机镜头的显式动态模拟&#xff0c;以及对光学性能的影响。使用Ansys Mechanical和LS-DYNA对相机在地板上的一系列冲击和弹跳过程…...

工具收集 - java-decompiler / jd-gui

工具收集 - java-decompiler / jd-gui 参考资料 用法&#xff1a;拖进来就行了 参考资料 https://github.com/java-decompiler/jd-gui 脚本之家&#xff1a;java反编译工具jd-gui使用详解...

《无线重构世界》射频模组演进

射频前端四大金刚 射频前端由PA、LNA、滤波器、开关“四大金刚” 不同的模块有自己的工艺和性能特点 分层设计 射频前端虽然只由PA、LNA、开关、混频器4个模块构成&#xff0c;但不同模块之间相互连接且相互影响。如果将射频系统当成一个整体来理解&#xff0c;其中的细节和…...

渗透测试---docker容器

声明&#xff1a;学习素材来自b站up【泷羽Sec】&#xff0c;侵删&#xff0c;若阅读过程中有相关方面的不足&#xff0c;还请指正&#xff0c;本文只做相关技术分享,切莫从事违法等相关行为&#xff0c;本人一律不承担一切后果 目录 一、Docker的作用与优势 二、docker的核心…...

【go从零单排】Atomic Counters原子计数

&#x1f308;Don’t worry , just coding! 内耗与overthinking只会削弱你的精力&#xff0c;虚度你的光阴&#xff0c;每天迈出一小步&#xff0c;回头时发现已经走了很远。 &#x1f4d7;概念 在 Go 语言中&#xff0c;原子计数器&#xff08;Atomic Counters&#xff09;是…...

VSCode中python插件安装后无法调试

问题 VSCode中python插件安装后无法调试&#xff0c;如下&#xff0c;点击调试&#xff0c;VScode中不报错&#xff0c;也没有调试 解决方法 1、查看配置 打开所在路径 2、拷贝 将整个文件夹拷贝到vscode默认路径下 3、问题解决 再次调试&#xff0c;可以正常使用了…...

用react实现radio同时关联proform组件

实现&#xff1a; <ProFormRadio.GroupradioType{button}name{[bodyConfig, format]}label"请求体格式"initialValue{json}options{createTabs}fieldProps{{buttonStyle: solid,wrapperMarginInlineEnd: 20,onChange: e > {let v e.target.value;databaseMod…...

Objective-C 1.0和2.0有什么区别?

Objective-C ObjC比较小众&#xff0c;在1980年左右由Stepstone公司的Brad Cox和Tom Love发明。后来NeXT公司获得ObjC语言使用权&#xff0c;再后来到1996年NeXT被苹果公司收购也变成苹果公司使用&#xff0c;Mac市场占有率本身就不高&#xff0c;ObjC没有太多程序员。在移动互…...

TCP连接如何保障数据传输安全

TCP连接保障数据传输安全主要通过以下几个机制实现&#xff1a; 1、建立连接&#xff1a;TCP通过三次握手过程建立连接&#xff0c;确保通信双方实体真实存在&#xff0c;为后续的可靠数据传输打下基础。 2、序号机制&#xff1a;TCP为每个传输的数据包分配一个序号&#xff…...

【论文复现】ChatGPT多模态命名实体识别

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀ChatGPT ChatGPT辅助细化知识增强&#xff01;1. 研究背景2. 模型结构和代码3. 任务流程第一阶段&#xff1a;辅助精炼知识启发式生成第二阶段…...

管理 Elasticsearch 变得更容易了,非常容易!

作者&#xff1a;来自 Elastic Ken Exner Elasticsearch 用户&#xff0c;我们听到了你的心声。管理 Elasticsearch 有时会变得很复杂&#xff0c;面临的挑战包括性能调整、问题检测和资源优化。我们一直致力于简化你的体验。今天&#xff0c;我们宣布了自收购 Opster 以来的一…...

SynchronousQueueworkQueue.offer() 和 poll() 方法的超时控制及线程池的讨论

2024.11.12 11:26 问&#xff1a;workQueue.offer(command) 这里 workQueue.poll(keepAlive, TimeUnit.NANOSECONDS) : 这里的 workQueue.offer(command) 没有超时时间控制&#xff1f; Claude 3.5 Haiku 非常好的问题&#xff01;我来详细解释 offer() 和 poll() 方法的区别…...

自动驾驶---“火热的”时空联合规划

1 背景 早期的不少规划算法都是横纵分离的(比如Apollo),先求解path之后,依赖path的结果再进行speed的求解。这种横纵解耦的规划方式具有以下特点: 相对较为简单,计算量通常较小,容易实现实时性要求。但是由于分别规划横向和纵向运动,可能会忽略两者之间的耦合关系,导致…...

命令行工具进阶指南

&#x1f680; 命令行工具进阶指南&#xff1a;Git、Shell与效率工具的进阶之路 掌握命令行工具&#xff0c;让你的开发效率突飞猛进。本文将深入探讨 Git 高级技巧、Shell 脚本自动化以及各种效率倍增的 CLI 工具。 &#x1f4d1; 目录 Git 高级技巧与工作流Shell 脚本自动化…...

扫雷游戏代码分享(c基础)

hi , I am 36. 代码来之不易&#x1f44d;&#x1f44d;&#x1f44d; 创建两个.c 一个.h 1&#xff1a;test.c #include"game.h"void game() {//创建数组char mine[ROWS][COLS] { 0 };char show[ROWS][COLS] { 0 };char temp[ROWS][COLS] { 0 };//初始化数…...

Leetcode 3576. Transform Array to All Equal Elements

Leetcode 3576. Transform Array to All Equal Elements 1. 解题思路2. 代码实现 题目链接&#xff1a;3576. Transform Array to All Equal Elements 1. 解题思路 这一题思路上就是分别考察一下是否能将其转化为全1或者全-1数组即可。 至于每一种情况是否可以达到&#xf…...

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

前端倒计时误差!

提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...

JVM垃圾回收机制全解析

Java虚拟机&#xff08;JVM&#xff09;中的垃圾收集器&#xff08;Garbage Collector&#xff0c;简称GC&#xff09;是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象&#xff0c;从而释放内存空间&#xff0c;避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

定时器任务——若依源码分析

分析util包下面的工具类schedule utils&#xff1a; ScheduleUtils 是若依中用于与 Quartz 框架交互的工具类&#xff0c;封装了定时任务的 创建、更新、暂停、删除等核心逻辑。 createScheduleJob createScheduleJob 用于将任务注册到 Quartz&#xff0c;先构建任务的 JobD…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

华为云Flexus+DeepSeek征文|DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建

华为云FlexusDeepSeek征文&#xff5c;DeepSeek-V3/R1 商用服务开通全流程与本地部署搭建 前言 如今大模型其性能出色&#xff0c;华为云 ModelArts Studio_MaaS大模型即服务平台华为云内置了大模型&#xff0c;能助力我们轻松驾驭 DeepSeek-V3/R1&#xff0c;本文中将分享如何…...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

Maven 概述、安装、配置、仓库、私服详解

目录 1、Maven 概述 1.1 Maven 的定义 1.2 Maven 解决的问题 1.3 Maven 的核心特性与优势 2、Maven 安装 2.1 下载 Maven 2.2 安装配置 Maven 2.3 测试安装 2.4 修改 Maven 本地仓库的默认路径 3、Maven 配置 3.1 配置本地仓库 3.2 配置 JDK 3.3 IDEA 配置本地 Ma…...