postgresql安装
- 安装方式:编译安装
- 操作系统:linux
- PostgreSQL版本:v14.6
- 用户:postgres
- 安装位置: /usr/local/pgsql
- data位置:/home/postgres/data
一、准备环境
1,创建新用户,PostgreSQL不允许使用root用户运行服务,需要创建新用户用于管理PostgreSQL服务,这里和保持默认的一直,创建新用户名为postgres
创建群组 postgres
- groupadd postgres
创建用户postgres,并指定群组为postgres
- useradd -g postgres postgres
2,下载PostgreSQL源码,点击 PostgreSQL 进入源码版本选择页,可以选择相应的版本,我选择v11.11
###切换到root用户
- su root
进入操作目录 /opt
- cd /opt
下载 PostgreSQL
- https://ftp.postgresql.org/pub/source/v14.6/
解压
- tar zxvf postgresql-14.6.tar.gz
切换目录
- cd postgresql-14.6
二、编译安装
- ./configure --without-readline
- make
- make install
- /usr/local/pgsql/bin/pg_ctl -V
如果显示pg_ctl (PostgreSQL) 14.6 以及类似的消息,只要不是表示已经安装成功了。但也只是表示安装成功了,接下来需要配置服务,这样在以后开机的时候就自动启动,就可以通过客户端访问了。
配置环境变量
export PG_HOME=/usr/local/pgsql
export PATH=$PATH:$PG_HOME/bin
三、配置服务
linux使用systemd启动守护进程,systemd默认的服务目录为 /lib/systemd/system,安装PostgreSQL服务需要在该目录下创一个文件,为了和运行的软件保持一致,这里命名为postgresql-14.service 在该文件中添加如下内容。
创建服务
archlinux使用systemd启动守护进程,systemd默认的服务目录为 /lib/systemd/system,安装PostgreSQL服务需要在该目录下创一个文件,为了和运行的软件保持一致,这里命名为postgresql-14.service 在该文件中添加如下内容。
- cd /lib/systemd/system
- vim /lib/systemd/system/postgresql-14.service
``
[Unit]
Description=The PostgreSQL Database Server
After=syslog.target
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/local/pgsql/bin/pg_ctl -D /home/postgres/data -l /home/postgres/logfile start
ExecStop=/usr/local/pgsql/bin/pg_ctl stop
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D /home/postgres/data
TimeoutSec=300
[Install]
WantedBy=multi-user.target
#数据库操作及远程访问
-
初始化数据库
-
initdb -D /home/postgres/data -k
-
启动数据库
-
pg_ctl -D /home/postgres/data -l /home/postgres/logfile start
-
停止数据库
-
pg_ctl -D /home/postgres/data stop
-
重启数据库
-
pg_ctl -s -D /home/postgres/data reload
-
如果需要远程机器连接需要修改 /home/postgres/data/pg_hba.conf 加入允许访问的地址
-
如果想在监听所有的ip地址需要修改 /home/postgres/data/postgresql.conf 修改listen_address地址为"0.0.0.0"