PostgreSQL基础+部署
什么是postgreSQL
PostgreSQL是一个功能强大的开源对象关系型数据库系统,他使用和扩展了SQL语言,并结合了许多安全存储和扩展最复杂数据工作负载的功能。PostgreSQL的起源可以追溯到1986年,作为加州大学伯克利分校POSTGRES项目的一部分,并且在核心平台上进行了30多年的积极开发。
PostgresSQL凭借其经过验证的架构,可靠性,数据完整性,强大的功能集,可扩展性以及软件背后的开源社区的奉献精神赢得了良好的声誉,以始终如一地提供高性能和创新的解决方案。PostgreSQL在所有主要操作系统开始使用PostgreSQL从未如此简单。
为什么要使用PostgreSQL
PostgreSql提供了许多功能,旨在帮助开发人员构建应用程序,管理员保护数据完整性并且构建容错环境,并帮助你管理数据,无论数据集的大小。除了免费和开源之外,Postgre SQL还具有高度的可扩展性。例如,你可以定义自己的数据类型,构建自定义SQL函数(此sql函数在另一篇帖子讨论:点此跳转),甚至可以编写来自不同编程语言的代码,而不需要重新编译数据库。
PostgreSql试图符合SQL标准,在这种标准中,这种一致性不会与传统特性相矛盾,或者可能导致糟糕的架构决策。支持SQL标准所需的许多功能,但是有时候语法或者功能略有不同。随着时间的推移,可以预期进一步向一致性迈进。从2018年10月发布的11版本开始,PostgreSQL符合SQL:2011核心一致性的179个强制性功能中的至少160个,在此之前,没有任何关系型数据库符合此标准的完全符合。
PostgreSQL vs MySQL:有什么区别?
PostgreSQL 和 MySQL 之间有很多不同之处。特性、功能和优势方面的一些差异如下:
- 数据库类型
- MySQL:关系型
- PostgreSQL:对象关系
- 编程语言
- MySQL: C/C++
- PostgreSQL: C
- 支持级联
- MySQL:没有
- PostgreSQL:是的
- 用户界面
- MySQL:工作台 GUI
- PostgreSQL: PgAdmin
- 支持的过程复杂度
- MySQL: SQL 语法和存储过程
- PostgreSQL:高级过程和存储过程
- 支持的索引类型
- MySQL:二叉搜索树(B-Tree)
- PostgreSQL:很多,包括 GIN 和 Hash
- 客户端和服务器之间的加密
- MySQL:传输层安全 (TLS) 协议
- PostgreSQL: SSL
- XML 数据类型支持
- MySQL:没有
- PostgreSQL:是的
- 支持物化视图和表继承
- MySQL:没有
- PostgreSQL:是的
- 支持高级数据类型
- MySQL:没有
- PostgreSQL:是的——hstore 和用户定义的 tdtaa
- 支持多版本并发控制 (MVCC)
- MySQL:没有
- PostgreSQL:是的
总之,PostgreSQL 和 MySQL 都有不同的用途,它们之间的选择取决于企业目标和资源。一般来说,PostgreSQL 是一个更强大、更高级的数据库管理系统,非常适合需要在大型环境中快速执行复杂查询的组织。但是,对于预算和空间更受限制的公司来说,MySQL 是一个理想的解决方案。
部署:
下载地址:sql
安装依赖
#安装依赖
[root@bogon ~]# yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
解压安装包并安装
[root@bogon ~]# tar -zxvf postgresql-15.2.tar.gz
[root@bogon ~]# mv postgresql-15.2 /usr/src/
[root@bogon ~]# cd /usr/src/postgresql-15.2/
[root@bogon postgresql-15.2]# ./configure --prefix=/usr/local/postgresql
[root@bogon postgresql-15.2]# make && make install
[root@bogon postgresql-15.2]# cd /usr/local/postgresql/
[root@bogon postgresql]# ls
bin include lib share
创建用户和组
[root@bogon postgresql]# groupadd postgres
[root@bogon postgresql]# useradd -g postgres postgres
[root@bogon postgresql]# id postgres
uid=1000(postgres) gid=1000(postgres) 组=1000(postgres)
创建postgresql数据库的数据主目录并修改文件所有者
[root@bogon postgresql]# cd
[root@bogon ~]# mkdir -p /postgresql/data
[root@bogon ~]# chown postgres:postgres /postgresql/data
配置环境变量
[root@bogon ~]# cd /home/postgres
[root@bogon postgres]# ls -al
总用量 12
drwx------ 2 postgres postgres 62 4月 23 09:55 .
drwxr-xr-x. 3 root root 22 4月 23 09:55 ..
-rw-r--r-- 1 postgres postgres 18 4月 1 2020 .bash_logout
-rw-r--r-- 1 postgres postgres 193 4月 1 2020 .bash_profile
-rw-r--r-- 1 postgres postgres 231 4月 1 2020 .bashrc
[root@bogon postgres]# vi .bash_profile
#最后添加
export PGHOME=/usr/local/postgresql
export PGDATA=/postgresql/data
PATH=$PATH:$HOME/bin:$PGHOME/bin[root@bogon postgres]# source .bash_profile
切换用户到postgres并使用initdb初使用化数据库
[root@bogon postgres]# su - postgres
[postgres@bogon ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".Data page checksums are disabled.fixing permissions on existing directory /postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:pg_ctl -D /postgresql/data -l logfile start[postgres@bogon ~]$ cd /postgresql/data/
[postgres@bogon data]$ ls
base pg_hba.conf pg_notify pg_stat pg_twophase postgresql.auto.conf
global pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
pg_commit_ts pg_logical pg_serial pg_subtrans pg_wal
pg_dynshmem pg_multixact pg_snapshots pg_tblspc pg_xact
配置服务
修改/pgsql/postgresql/data目录下的两个文件
postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
pg_hba.conf 配置对数据库的访问权限。
[postgres@bogon data]$ vim postgresql.conf
#找到listen_addresses
listen_addresses = '*' # 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)[postgres@bogon data]$ vim pg_hba.conf
#在最下面找到
# IPv4 local connections:
#将这行加进去,这样局域网的人才能访问。下面那行的默认就有。
host all all 0.0.0.0/0 trust
host all all 127.0.0.1/32 trust
设置PostgreSQL开机自启动
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
linux文件即为linux系统上的启动脚本
[postgres@bogon contrib]$ cd /usr/src/postgresql-15.2/contrib/start-scripts/
[postgres@bogon start-scripts]$ ls
freebsd linux macos#切换至root用户授权
[root@bogon start-scripts]# chmod a+x linux#复制linux文件到/etc/init.d目录下,更名为postgresql
[root@bogon start-scripts]# cp linux /etc/init.d/postgresql#修改/etc/init.d/postgresql文件的两个变量
[root@bogon ~]# vim /etc/init.d/postgresql
# Installation prefix
prefix=/usr/local/postgresql/
# Data directory
PGDATA="/postgresql/data/"[root@bogon ~]# chkconfig --add postgresql
[root@bogon ~]# chkconfig 注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。查看在具体 target 启用的服务请执行'systemctl list-dependencies [target]'。netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
postgresql 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@bogon ~]#
开启postgresql
[root@bogon ~]# systemctl start postgresql.service
[root@bogon ~]# systemctl status postgresql.service
● postgresql.service - SYSV: PostgreSQL RDBMSLoaded: loaded (/etc/rc.d/init.d/postgresql; bad; vendor preset: disabled)Active: active (exited) since 日 2023-04-23 10:21:51 CST; 3s agoDocs: man:systemd-sysv-generator(8)Process: 21031 ExecStart=/etc/rc.d/init.d/postgresql start (code=exited, status=0/SUCCESS)4月 23 10:21:51 bogon systemd[1]: Starting SYSV: PostgreSQL RDBMS...
4月 23 10:21:51 bogon su[21032]: (to postgres) root on none
4月 23 10:21:51 bogon systemd[1]: Started SYSV: PostgreSQL RDBMS.
4月 23 10:21:51 bogon postgresql[21031]: Starting PostgreSQL: ok或者用这个
[root@bogon ~]# service postgresql start
Starting PostgreSQL: ok
开始测试
[root@bogon ~]# su - postgres
上一次登录:日 4月 23 10:22:58 CST 2023pts/0 上
[postgres@bogon ~]$ psql
psql (15.2)
Type "help" for help.postgres=#
postgres-# \\lList of databasesName | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Accessprivileges
-----------+----------+----------+-------------+-------------+------------+-----------------+---------
--------------postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | =c/postg
res +| | | | | | | postgres
=CTc/postgrestemplate1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | =c/postg
res +| | | | | | | postgres
=CTc/postgres
(3 rows)
创建用户
postgres=# create user lyh password 'lyh';
CREATE ROLE
postgres=# alter role lyh superuser;
ALTER ROLE
postgres=# create database lyh;
CREATE DATABASE
postgres=# \\q#重新登录数据库
[postgres@bogon ~]$ psql -U lyh -d lyh
psql (15.2)
Type "help" for help.lyh=#
lyh=# \\lList of databasesName | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Accessprivileges
-----------+----------+----------+-------------+-------------+------------+-----------------+---------
--------------lyh | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | =c/postg
res +| | | | | | | postgres
=CTc/postgrestemplate1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | libc | =c/postg
res +| | | | | | | postgres
=CTc/postgres
(4 rows)lyh=#