CentOS系统下PostgreSQL数据库安装配置

版权声明:转载请注明出处! https://blog.csdn.net/ywd1992/article/details/81116137

一、基础环境

1、操作系统:CentOS 7.3

2、PostgreSQL版本:10.3  链接:https://pan.baidu.com/s/1vY6IWb9N8NCG3MenQWXi8w 密码:sqdy

二、安装

1、安装依赖环境

yum -y install readline readline-devel zlib-devel

2、编译安装PostgreSQL

tar -xvf postgresql-10.3.tar.gz

cd postgresql-10.3

./configure
make && make install

PostgreSQL默认安装目录为 /usr/local/pgsql/

默认端口:5432

3、创建用户及相关目录

adduser postgres
passwd postgres

建议数据及日志目录

mkdir -p /usr/local/pgsql/data
mkdir -p /usr/local/pgsql/logs

更改文件夹所有者为用户postgres

chown postgres /usr/local/pgsql/data/
chown postgres /usr/local/pgsql/logs/

4、初始化数据库

su "postgres"

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/

5、启动数据库

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/logs/postgres.log start

查看服务是否启动成功

ps -ef | grep postgres

6、如果想对数据库进行配置,可编辑配置文件postgresql.conf

vi /usr/local/pgsql/data/postgresql.conf

listen_addresses = '*'

vi pg_hba.conf

host    all             all             0.0.0.0/0            trust

重启数据库使配置生效

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/logs/postgres.log restart

7、修改postgres用户的访问密码并测试建库建表

PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码

cd /usr/local/pgsql/bin

./psql

ALTER user postgres with password '123456';

测试建库建表

select * from pg_shadow ;
create database gsum;
\c gsum

create table person(id integer, name text);
insert into person values (1,'darren');
select * from person;

退出数据库 \q

8、设置开机自启

root用户下

找到安装包内的linux文件

cd /postgresql-10.3/contrib/start-scripts/

chmod a+x linux

编辑linux文件

vi linux

安装目录
prefix=/usr/local/pgsql
数据目录
PGDATA="/usr/local/pgsql/data"

复制linux文件到/etc/init.d目录下,更名为postgresql

cp linux /etc/init.d/postgresql

设置开机自启即可

chkconfig --add postgresql

猜你喜欢

转载自blog.csdn.net/ywd1992/article/details/81116137