pg安装入门

Posted charon2

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pg安装入门相关的知识,希望对你有一定的参考价值。

1.下载源码安装包

[[email protected] ~]# wget https://ftp.postgresql.org/pub/source/v10.5/postgresql-10.5.tar.gz

2.创建pg的用户主、组

[[email protected] postgresql-10.5]# useradd postgres
[[email protected] postgresql-10.5]# groupadd postgres
[[email protected] postgresql-10.5]# passwd postgres

3.解压、进入目录

[[email protected] postgresql-10.5]# tar zxvf postgresql-10.5.tar.gz 
[[email protected] postgresql-10.5]# cd postgresql-10.5

4.创建postgreSQL的安装目录

[[email protected] postgresql-10.5]# mkdir /usr/local/postgresql

5.下载依赖包

[[email protected] postgresql-10.5]# yum -y install -y readline-devel

6.预编译#-prefix是指定postgreSQL安装路径

[[email protected] postgresql-10.5]# ./configure --prefix=/usr/local/postgresql

 7.编译安装

[[email protected] postgresql-10.5]# make
[[email protected] postgresql-10.5]# make install

显示这个说明成功

技术图片

8.安装contrib目录下的一些工具,是第三方组织的一些工具代码,建议安装

[[email protected] postgresql-10.5]# cd contrib
[[email protected] contrib]# make && make install

9.创建相关目录

♦数据目录

[[email protected] contrib]# mkdir -p /usr/local/postgresql/data

♦日志目录

[[email protected] contrib]# mkdir -p /usr/local/postgresql/logs

9. 赋予postgres用户相关文件夹权限

[[email protected] postgresql-10.5]# chown -R postgres:postgres /usr/local/postgresql

 10.配置环境变量

[[email protected] postgresql-10.5]# cat /etc/profile.d/pgsql.sh 
export PATH=$PATH:/usr/local/postgresql/bin/

[[email protected] postgresql-10.5]# source /etc/profile.d/pgsql.sh

 11.启动数据库

[[email protected] postgresql-10.5]# su postgres
初始化数据库
[[email protected] postgresql-10.5]$ initdb -D /usr/local/postgresql/data/
启动服务
pg_ctl -D /var/postgresql/data -l /var/postgresql/logs/logfile start
连接数据库
[[email protected] postgresql-10.5]$ psql 
创建数据库 
postgres=# create database test; 
创建表
postgres=# create table t_user (id integer, name text); 
插入测试数据
postgres=# insert into t_user values (1,‘joke‘);
查询数据
postgres=# select * from t_user;
退出psql窗口
postgres-# q

 12.修改监听所有网络以及数据库连接数

[[email protected] postgresql-10.5]$ vim /usr/local/postgresql/data/postgresql.conf 
 60 listen_addresses = ‘*‘          # what IP address(es) to listen on;
 65 max_connections = 100                   # (change requires restart)

13.修改远程访问

[[email protected] postgresql-10.5]$ vim /usr/local/postgresql/data/pg_hba.conf 
#在文件的最下方加上下面的这句话
host    all         all         0.0.0.0/0             trust

如下

[[email protected] postgresql-10.5]$ tail -n 6 /usr/local/postgresql/data/pg_hba.conf
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    all             all             0.0.0.0/0               trust

 14.防火墙开启端口

# 切换root用户
su - root
# 防火墙 允许5432 端口

 15.重启postgreSQL服务

[[email protected] postgresql-10.5]# su - postgres
[[email protected] ~]$ pg_ctl -D /usr/local//postgresql/data/ -l /usr/local/postgresql/logs/logfile restart

停止服务命令 

[[email protected] ~]$ pg_ctl -D /usr/local//postgresql/data/ -l /usr/local/postgresql/logs/logfile stop

 16.设置开机自启动

切换到root用户
[[email protected] ~]$ su root
找到解压后源码包里面的一个linux文件
[[email protected] postgres]# chmod a+x /data/postgresql-10.5/contrib/start-scripts/linux
复制linux文件到/etc/init.d目录下,更名为postgresql
[[email protected] postgres]# cp /data/postgresql-10.5/contrib/start-scripts/linux /etc/init.d/postgresql

 17.修改/etc/init.d/postgresql文件的两个变量

31 # Installation prefix
32 prefix=/usr/local/postgresql 33 34 # Data directory 35 PGDATA="/usr/local/postgresql/data" 37 # Who to run the postmaster as, usually "postgres". (NOT "root") 38 PGUSER=postgres

技术图片

18.执行service postgresql start,可以启动PostgreSQL服务

19.设置postgresql服务开机自启动

[[email protected] postgres]# chkconfig --add postgresql
[[email protected] postgres]# chkconfig --level 2345 postgresql on 
[[email protected] postgres]# chkconfig --list

 

以上是关于pg安装入门的主要内容,如果未能解决你的问题,请参考以下文章

Mac + virtualenv + pip + postgresql = 错误:找不到 pg_config 可执行文件

推荐net开发cad入门阅读代码片段

pg快速入门--配置文件

[转帖]PG语法解剖--基本sql语句用法入门

pg快速入门--初步使用

pg快速入门--体系结构