postgresql数据库安装

469 阅读3分钟

一、 配置linux环境

yum install -y wget  # 安装wget
# 配置阿里云镜像源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
# 这个镜像同样适用于AnolisOS系统
yum clean all && yum makecache
yum install -y lrzsz # 安装上传下载工具 
yum install -y tmux
yum install -y lsof
yum install -y net-tools
yum 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 postgres
useradd -g postgres postgres
id postgres
# uid=1001(postgres) gid=1001(postgres) 组=1001(postgres)
# 安装数据库
./configure --prefix=/usr/local/pgsql/
make && make istall
chown postgres -R /usr/local/pgsql
chown 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 postgres
vi .bash_profil
    export PGHOME=/usr/local/pgsql/
    export PGDATA=/usr/local/pgsql/data
    PATH=$PATH:$PGHOME/bin:$PGDATA/bin
source .bash_profile

四、initdb初始化数据库

[postgres@localhost ~]$ 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 /usr/local/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/pgsql/data -l logfile start
    
[postgres@localhost data]$ cd /usr/local/pgsql/data
[postgres@localhost 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 配置对数据库的访问权限。

# 修改配置文件
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 14608
postgres   14608   14542  0 23:28 pts/0    00:00:00 postgres -D /usr/local/pgsql/data
postgres   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

六、测试

image-20220721234456803.png