
PostgreSQL安装
PostgreSQL安装
Linux(CentOs)安装方法
使用yum安装
安装存储库RPM:
1 | yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm |
安装客户端软件包:
1 | yum install postgresql10 |
安装服务器包:
1 | yum install postgresql10-server |
初始化数据库并启用自动启动:
1 | / usr / pgsql-10 / bin / postgresql-10-setup initdb |
使用tar包安装
准备环境
在进行安装之前,需要创建一个用户与组,否则在初始化数据库的时候会报如下错误:
1 | Running in debug mode. |
新建组与用户
1 | groupadd postgres |
在安装之前需要安装以下:
make、gcc、gcc-c++、zlib-devel、readline-devel
1 | [root@linhp local]# yum -y install gcc |
如果有,请勿重复安装
开始安装
首先前往官网下载安装包,下载地址:https://www.postgresql.org/ftp/source/
你可以选择任意版本,不过不推荐选择beta
版
1 | [root@localhost local]# wget https://ftp.postgresql.org/pub/source/v10.4/postgresql-10.4.tar.gz |
编译安装
1 | [root@localhost postgresql]# ./configure --prefix=/usr/local/postgresql --without-readline |
将postgrepsql加入到环境变量
1 | [root@localhost contrib]# vim /etc/profile |
1 | export PGHOME=/usr/local/postgresql |
1 | [root@localhost contrib]# source /etc/profile |
初始化数据库
1 | [root@localhost postgresql]# su - postgres |
启动数据库服务
1 | [postgres@localhost postgresql]$ pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/logfile start |
连接数据库
使用psql
进入控制台
1 | [postgres@localhost postgresql]$ psql |
在控制台中进行一下操作
1 | postgres=# create database test; |
顺便一提,在postgre控制台,退出需要使用\q
命令
远程访问
修改PostgreSql配置文件
1 | [postgres@localhost postgresql]$ vim data/postgresql.conf |
在其最下方添加一行
1 | host all all 0.0.0.0/0 trust |
不过出安全,不推荐这样做
创建超级用户
1 | [postgres@localhost postgresql]$ createuser -P -s -U postgres -p 5432 psql |
辅助脚本
为了方便操作,这里写三个命令,你可以把这个三个命令写到脚本文件里,这样可以方便执行
启动命令
1 | pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/logfile start |
重启命令
1 | pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/logfile restart |
停止命令
1 | pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/logfile stop |
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自jarome