PostgreSQL开发与实战(1)安装部署

云的事随心讲 2024-02-27 15:17:16
作者:太阳PostgreSQL是一款开源的高度可扩展、跨平台的关系型数据库管理系统,支持复杂数据类型、ACID事务、触发器、存储过程等特性,具备强大的查询优化器和完整性约束,拥有庞大的全球社区支持。它适用于各种规模和类型的应用,提供稳定可靠的数据库解决方案。下面将分别介绍 PostgreSQL的rpm包安装和源码安装部署的详细步骤。 一、rpm包安装部署1、安装RPM包 # yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm# yum install -y postgresql12-server# rpm -qa | grep postgrespostgresql12-12.4-1PGDG.rhel7.x86_64postgresql12-libs-12.4-1PGDG.rhel7.x86_64postgresql12-server-12.4-1PGDG.rhel7.x86_642、添加用户 # groupadd postgres# useradd -g postgres postgres3、配置数据、日志目录 # mkdir -p /data/pgsql/{data,logs}# chown -R postgres:postgres /data/pgsql4、初始化数据库 # su - postgres$$ /usr/pgsql-12/bin/initdb -E utf8 -D /data/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 "en_US.UTF-8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /data/pgsql/data ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default time zone ... Asia/Shanghaicreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connectionsYou 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: /usr/pgsql-12/bin/pg_ctl -D /data/pgsql/data/ -l logfile start5、启动数据库 $ /usr/pgsql-12/bin/postgres -D /data/pgsql/data/ > /data/pgsql/logs/postgres.log &[1] 185346、创建用户和数据库 $ /usr/pgsql-12/bin/psqlpsql (12.4)Type "help" for help.postgres=# create user sansi with password '123';CREATE ROLEpostgres=# create database db1 with encoding='utf8' owner='sansi';CREATE DATABASE#验证登录$ /usr/pgsql-12/bin/psql -U sansi -d db1psql (12.4)7、环境变量配置 $ cat /home/postgres/.bash_profile# .bash_profile# Get the aliases and functionsif [ -f ~/.bashrc ]; then . ~/.bashrcfi# User specific environment and startup programsPATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/binexport PATHexport PGHOME=/usr/local/pgsql #软件安装目录export PGDATA=/data/pgsql12/data #PG数据目录$ source /home/postgres/.bash_profile二、源码安装1、安装源码软件包 # yum install lbzip2# wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2# tar xf postgresql-12.2.tar.bz2 -C /usr/local/2、安装依赖包 # yum install gcc gcc-c++ readline-devel readline readline-dev zlib-devel3、编译安装 # cd /usr/local/postgresql-12.2/# ./configure --prefix=/usr/local/pgsql# make && make install编译时一些必要的参数: -with-perl : 编译时添加该参数才能够使用perl语法的PL/Perl过程语言写自定义函数,一般都需要。需要提前安装好相关的perl开发包:libperl-dev-with-python : 编译时添加该参数才能够使用python语法的PL/Perl过程语言写自定义函数,一般都需要。需要提前安装好相关的python开发包:python-dev-with-blocksize=128 --with-wal-blocksize=128 : 默认情况下PG数据库的数据页大小为8KB,若数据库用来做数仓业务,可在编译时将数据页进行调整,以提高磁盘IO-enable-thread-safety : 保证客户端线程安全,该参数在PG 9.0以上默认线程安全,如果安装的是8.0版本需要手动指定该参数4、添加用户 # groupadd postgres# useradd -g postgres postgres5、配置数据、日志目录 # mkdir -p /data/pgsql12/{data,logs}# chown -R postgres:postgres /data/pgsql12/6、配置环境变量 # vi /etc/profileexport PGHOME=/usr/local/pgsqlexport PGDATA=/data/pgsql12/dataexport PATH=$PGHOME/bin:$PATHexport LD_LIBRARY_PATH=/usr/local/pgsql/lib# source /etc/profile7、初始化数据库 # su - postgresLast login: Tue Sep 1 22:52:40 CST 2020 on pts/0$ initdb -D /data/pgsql12/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 "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".Data page checksums are disabled.fixing permissions on existing directory /data/pgsql12/data ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default time zone ... Asia/Shanghaicreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connectionsYou 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 /data/pgsql12/data/ -l logfile start8、启动数据库 $ pg_ctl -D /data/pgsql12/data/ -l logfile startwaiting for server to start.... doneserver started9、验证登录并修改用户验证方式 在对数据库做初始化时会创建一个与操作系统同名的用户在数据库的超级用户,所以在改OS用户下登录数据库默认不需要使用账户和密码,当然也可以通过修改 pg_hba.conf 配置文件进行控制。 ## 通过postgres用户免密登录数据库,并修改该数据库用户密码$ psqlpsql (12.2)Type "help" for help.postgres=# alter user postgres password '123';ALTER ROLEpostgres=# \q## 修改认证配置文件,要求全部进行md5认证(将trust改为md5)local all all md5host all all 127.0.0.1/32 md5## 修改配置文件后reload重新载入配置文件$ pg_ctl reload## 再次验证是否必须密码登录$ psqlPassword for user postgres: //输入密码psql (12.2)Type "help" for help.更多技术信息请查看云掣官网云掣YunChe - 可观测运维专家 | 大数据运维托管 | 云MSP服务
0 阅读:9

云的事随心讲

简介:感谢大家的关注