如何安装PostgreSQL数据库?

发表时间: 2022-07-21 23:52

一、 配置linux环境

yum install -y wget  # 安装wget# 配置阿里云镜像源mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backupwget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo# 这个镜像同样适用于AnolisOS系统yum clean all && yum makecacheyum install -y lrzsz # 安装上传下载工具 yum install -y tmuxyum install -y lsofyum install -y net-toolsyum install -y tar

二、安装数据库

# 安装数据库安装环境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 gcc gcc-c++ make# 解压tar -zxvf postgresql-14.2.tar.gz # 创建数据目录mkdir -p /usr/local/pgsql/data# postgres创建用户groupadd postgresuseradd -g postgres postgresid postgres# uid=1001(postgres) gid=1001(postgres) 组=1001(postgres)# 安装数据库./configure --prefix=/usr/local/pgsql/make && make istallchown postgres -R /usr/local/pgsqlchown postgres.postgres -R /usr/local/pgsql/data

选项

描述

–prefix=prefix

安装到prefix指向的目录;默认为/usr/local/pgsql

–bindir=dir

安装应用程序到dir;默认为prefix/bin

–with-docdir=dir

安装文档到dir;默认为prefix/doc

–with-pgport=port

设置默认的服务器端网络连接服务TCP端口号

–with-tcl

为服务端提供Tcl存储过程支持

–with-perl

为服务端提供Perl存储过程支持

–with-python

为服务端提供Python存储过程支持

三、配置环境变量

su -l postgresvi .bash_profil    export PGHOME=/usr/local/pgsql/    export PGDATA=/usr/local/pgsql/data    PATH=$PATH:$PGHOME/bin:$PGDATA/binsource .bash_profile

四、initdb初始化数据库

[postgres@localhost ~]$ initdbThe 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/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:    pg_ctl -D /usr/local/pgsql/data -l logfile start    [postgres@localhost data]$ cd /usr/local/pgsql/data[postgres@localhost data]$ lsbase          pg_hba.conf    pg_notify     pg_stat      pg_twophase  postgresql.auto.confglobal        pg_ident.conf  pg_replslot   pg_stat_tmp  PG_VERSION   postgresql.confpg_commit_ts  pg_logical     pg_serial     pg_subtrans  pg_walpg_dynshmem   pg_multixact   pg_snapshots  pg_tblspc    pg_xact

五、修改配合文件并启动数据库

修改/pgsql/postgresql/data目录下的两个文件。

postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。

pg_hba.conf 配置对数据库的访问权限。

# 修改配置文件vi postgresql.conf    listen_addresses = '*' vi pg_hba.conf    # IPv4 local connections:    host    all             all             0.0.0.0/0               trust    host    all             all             127.0.0.1/32            trust    # 启动数据库[postgres@localhost bin]$ postgres -D /usr/local/pgsql/data >logfile 2>&1 &[1] 14608# 查看进程[postgres@localhost bin]$ ps -ef |grep 14608postgres   14608   14542  0 23:28 pts/0    00:00:00 postgres -D /usr/local/pgsql/datapostgres   14610   14608  0 23:28 ?        00:00:00 postgres: checkpointer postgres   14611   14608  0 23:28 ?        00:00:00 postgres: background writer postgres   14612   14608  0 23:28 ?        00:00:00 postgres: walwriter postgres   14613   14608  0 23:28 ?        00:00:00 postgres: autovacuum launcher postgres   14614   14608  0 23:28 ?        00:00:00 postgres: stats collector postgres   14615   14608  0 23:28 ?        00:00:00 postgres: logical replication launcher postgres   14619   14542  0 23:29 pts/0    00:00:00 grep --color=auto 14608# 查看端口[postgres@localhost bin]$ netstat -tunlp|grep 5432(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 0.0.0.0:5432            0.0.0.0:*               LISTEN      14608/postgres      tcp6       0      0 :::5432                 :::*                    LISTEN      14608/postgres   # 连接数据库测试[postgres@localhost bin]$ createdb test  # 创建test库[postgres@localhost bin]$ psql test # 进入数据库psql (14.2)Type "help" for help.test=# \l                                  List of databases   Name    |  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/postgres template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +           |          |          |             |             | postgres=CTc/postgres test      | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | (4 rows)test=# # 数据库重启pg_ctl restart -D /usr/local/pgsql/data# 数据库停止pg_ctl stop -D /usr/local/pgsql/data

六、测试