杨梅冲
每天在想什么呢?

一、PostgreSQL简介

官网:https://www.postgresql.org/
下载地址:https://www.postgresql.org/download/linux/redhat/
源码下载:https://www.postgresql.org/ftp/source/v15.1/
中文社区:http://www.postgres.cn/v2/home

1.1 简介

PostgreSQL数据库具有以下优势:
PostgreSQL数据库是目前功能最强大的开源数据库,它是最接近工业标准SQL92的查询语言,至少实现了SQL:2011标准中要求的179项主要功能中的160项(注:目前没有哪个数据库管理系统能完全实现SQL:2011标准中的所有主要功能)。
稳定可靠:PostgreSQL是唯一能做到数据零丢失的开源数据库。目前有报道称国内外有部分银行使用PostgreSQL数据库。
开源省钱: PostgreSQL数据库是开源的、免费的,而且使用的是类BSD协议,在使用和二次开发上基本没有限制。
支持广泛:PostgreSQL 数据库支持大量的主流开发语言,包括C、C++、Perl、Python、Java、Tcl以及PHP等。
PostgreSQL社区活跃:PostgreSQL基本上每3个月推出一个补丁版本,这意味着已知的Bug很快会被修复,有应用场景的需求也会及时得到响应

1.2 PostgreSQL和MySQL的比较

● 在SQL的标准实现上要比MySQL完善,而且功能实现较为严谨。

● 对表连接支持较为完整,优化器的功能较完整,支持的索引类型很多,复杂查询能力较强。

● PG主表采用堆表存放,MySQL采用索引组织表,能够支持比MySQL更大的数据量。(了解)

● PG的主备复制属于物理复制,相对于MySQL基于binlog的逻辑复制,数据的一致性更加可靠,复制性能更高,对主机性能的影响也更小。(底层了解)

● PostgreSQL支持JSON和其他的NoSQL功能,如本机XML支持和使用HSTORE的键值对。它还支持索引JSON数据加快访问速度,特别是10版本JSONB更是强大。

● PostgreSQL完全免费,而且是BSD协议,如果把PostgreSQL改一改,然后再拿去卖钱,也没有人管你,这一点很重要,这表明了PostgreSQL数据库不会被其他公司控制。相反,MySQL现在主要是被Oracle公司控制。

MySQL相对于PG的优势

● innodb的基于回滚段实现的MVCC机制,相对PG新老数据一起存放的基于XID的MVCC机制,是占优的。新老机制一起存放,需要定时触发VACUUM,会带来多余的IO和数据库对象加锁开销,引起数据库整体的并发能力下降。而且VACUUM清理不及时,还会引发数据膨胀。

● MySQL采用索引组织表,这种存储方法非常适合基于主键匹配的查询、删改操作,但是对表结构设计存在约束。

● MySQL的优化器较为简单,系统表、运算符、数据类型的实现都很精简,非常适合简单的查询操作。

● MySQL相对于PG在国内的流行度更高。

● MySQL的存储引擎插件化机制,使得它的应用场景更加广泛,比如除了innodb适合事务处理场景外,myisam适合静态数据的查询场景

总结:
从应用场景来说,PG更适合严格的企业应用场景(比如金融、电信、ERP、CRM),但不仅仅限制于此,PostgreSQL的json,jsonb,hstore等数据格式,特别适用于一些大数据格式的分析;
MySQL更加适合业务逻辑相对简单、数据可靠性要求较低的互联网场景(比如Google、Facebook、alibaba),当然现在MySQL在innodb引擎的大力发展,功能表现良好。

二、PostgreSQL安装

# yum安装
https://www.postgresql.org/download/linux/redhat/

# 官网安装方法
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
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
# 1.源码安装
yum -y install gcc gcc-c++ readline-devel zlib-devel
tar -xzvf postgresql-14.6.tar.gz
cd postgresql-14.6
./configure --prefix=/usr/local/postgresql-14.6
make && make install

# 2.创建用户以及数据存放目录
cd /usr/local/postgresql-14.6
useradd postgres
mkdir pgsql/data -p
chown -R postgres.postgres pgsql
ln -s /usr/local/postgresql-14.6 /usr/local/postgresql #
3.初始化数据库 su postgres # 进入postgres用户,使用root用户会报错 cd /usr/local/postgresql-14.6/bin ./initdb /usr/local/postgresql-14.6/pgsql/data """ [postgres@moban bin]$ ./initdb /usr/local/postgresql-14.6/pgsql/data 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 /usr/local/postgresql-14.6/pgsql/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 ... ok initdb: warning: enabling "trust" authentication for local connections 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 /usr/local/postgresql-14.6/pgsql/data -l logfile start """
# 4.启动服务
[postgres@moban pgsql]$ /usr/local/postgresql-14.6/bin/pg_ctl -D /usr/local/postgresql-14.6/pgsql/data -l logfile start
waiting for server to start.... done
server started

# 查看状态
[postgres@moban pgsql]$ /usr/local/postgresql-14.6/bin/pg_ctl -D /usr/local/postgresql-14.6/pgsql/data -l logfile status
pg_ctl: server is running (PID: 87445)
/usr/local/postgresql-14.6/bin/postgres "-D" "/usr/local/postgresql-14.6/pgsql/data"

[postgres@moban pgsql]$ netstat -ntlp | grep postgres
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      87481/postgres      
tcp6       0      0 ::1:5432                :::*                    LISTEN      87481/postgres

# 停止服务
[postgres@moban pgsql]$ /usr/local/postgresql-14.6/bin/pg_ctl -D /usr/local/postgresql-14.6/pgsql/data -l logfile stop
waiting for server to shut down.... done
server stopped

2.1 修改PostgreSQL配置文件

# 初始化后在postgresql的目录可以看到生成的数据目录data以及该目录的相关数据和配置文件,pg_hba.conf和postgresql.conf:
一个是访问控制配置(127.0.0.1改为信任的客户端ip网段使其可以远程访问)
一个是postgresql主配置文件(listen_address=localhost改为星号使其监听整个网络)

# pg_hba.conf中配置服务端允许的认证方式,添加下面一行:
host all all
0.0.0.0/0 password
或者改为:
host all all 0.0.0.0/0 md5 # 表示允许所有网络连接,trust改为md5,表示需要密码连接,采用md5加密
host    all             all             127.0.0.1/32            md5
# postgresql.conf编辑或添加下面一行,使PostgreSQL可以接受来自任意IP的连接请求,把端口开放 
listen_addresses
= '*'
port = 5432
# 重新启动
/usr/local/postgresql-14.6/bin/pg_ctl -D /usr/local/postgresql-14.6/pgsql/data -l logfile start

2.2 登录以及设置密码

# 修改postgres密码
cd /usr/local/postgresql/bin
[postgres@moban bin]$ ./psql 
psql (14.6)
Type "help" for help
postgres=# alter user postgres with password 'wg1q2w3e';
ALTER ROLE  # 修改成功

# 登录
[postgres@moban bin]$ ./psql -d postgres -p 5432 -U postgres -h localhost
psql (14.6)
Type "help" for help.

postgres=#

 

 2.3 设置开机启动

# Centos 6
# 进入编译目录
cd /usr/local/src/postgresql-14.6/contrib/start-scripts

# centos 6系统只需要拷贝到对应目录并添加权限
cp linux /etc/init.d/postgresql
chmod +x /etc/init.d/postgresql
# 修改对应目录
vim /etc/init.d/postgresql
prefix=/usr/local/postgresql
PGDATA="/usr/local/postgresql/pgsql/data"
# 开机启动
chkconfig --add postgresql
[root@moban ~]# /etc/init.d/postgresql status
pg_ctl: server is running (PID: 87567)
/usr/local/postgresql-14.6/bin/postgres "-D" "/usr/local/postgresql-14.6/pgsql/data"
[root@moban ~]# /etc/init.d/postgresql stop
Stopping PostgreSQL: ok
[root@moban ~]# /etc/init.d/postgresql start
Starting PostgreSQL: ok

 

posted on 2022-12-28 11:26  杨梅冲  阅读(242)  评论(0编辑  收藏  举报