安装postgresql数据库
yum -y install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm
yum -y install postgresql11-contrib postgresql11-server
创建数据目录,默认是/var/lib/pgsql,这里更改为/data/postgresql_data
mkdir /data/postgresql_data -p
chown postgres:postgres -R /data/postgresql_data/
chmod 750 /data/postgresql_data/
修改配置文件,更改数据目录
vim /usr/lib/systemd/system/postgresql-11.service
Environment=PGDATA=/data/postgresql_data
添加环境变量
vim /etc/profile
export PATH=/usr/pgsql-11/bin:$PATH
export LD_LIBRARY_PATH=/usr/pgsql-11/lib
export PGDATA=/data/postgresql_data
source /etc/profile
初始化postgresql
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl start postgresql-11.service
systemctl enable postgresql-11.service
netstat -atnup | grep 5432 #postgresql数据库默认端口5432
设置登录密码
su - postgres #进入postgres用户
-bash-4.2$ psql
psql (11.4)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD '311311';
ALTER ROLE
postgres=# \q
备注:\q退出数据库;\l列出所有用户;\du列出库下所有表
设置远程访问
vim /data/postgresql_data/pg_hba.conf #拉到最下面,更改如下
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication all md5
#host replication all 127.0.0.1/32 md5
#host replication all ::1/128 md5
host all all 0.0.0.0/0 md5
vim /data/postgresql_data/postgresql.conf #改为如下
重启:
[root@test ~]# systemctl restart postgresql-11
再进入postgresql就要密码了
使用pgAdmin工具远程连接