环境:Centos7.4
PostgreSQL-11.7可以在官网下载
一、创建postgres用户 (root)
useradd postgres
echo 'postgres:passwd' | sudo chpasswd
echo 'postgres ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
mkdir -p /app/postgres
chown postgres.postgres /app/postgres
chmod -R 775 /app
二、yum依赖包(root用户)
yum -y install unzip zip lrzsz vim readline readline-devel openssl openssl-devel zlib zlib-devel
三、查询关闭防火墙(root用户)
firewall-cmd --state
systemctl stop firewalld.service
systemctl disable firewalld.service
------------------------------------------------------------
或开通PostgreSQL端口:
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
------------------------------------------------------------
四、修改主机名称(root用户)
hostnamectl --static set-hostname test-postgres-001
五、检查安装环境 (root)
su postgres
mkdir postgresql
mkdir -p /app/postgres/postgresql/data
mkdir -p /app/postgres/postgresql/log
#切换root
sudo su -
# 进入解压后的源码文件夹(/app/postgresql-11.7)
cd /app/postgresql-11.7
# 检查、并配置(--prefix=/app/postgres/postgresql 为配置postgreSQL的安装路径)
./configure --prefix=/app/postgres/postgresql
------------------------------------------------------------------------------------
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
------------------------------------------------------------------------------------
六、编译及安装(root 时间较长)
make && make install
------------------------------------------------------------------------------------
make -C config install
make[1]: Entering directory `/app/postgresql-11.7/config'
/bin/mkdir -p '/app/postgres/postgresql/lib/pgxs/config'
/bin/install -c -m 755 ./install-sh '/app/postgres/postgresql/lib/pgxs/config/install-sh'
/bin/install -c -m 755 ./missing '/app/postgres/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/app/postgresql-11.7/config'
PostgreSQL installation complete.
[root@localhost postgresql-11.7]#
------------------------------------------------------------------------------------
#使用root授权
sudo chown -R postgres:postgres /app/postgres/postgresql
六、配置环境变量 (postgres)
su - postgres
切换到用户根目录下(/home/postgres)
cd ~
编辑 .bash_profile
vi ~/.bash_profile
export PGHOME=/app/postgres/postgresql
export PGDATA=/app/postgres/postgresql/data
export PGLOG=/app/postgres/postgresql/log/serverlog
export PATH=$PGHOME/bin:$PATH
source /etc/profile
七、初始化数据库(postgres)
使用postgres用户执行如下命令:
cd /app/postgres/postgresql/
initdb -D /app/postgres/postgresql/data -U postgres --locale=en_US.UTF8 -E UTF8
看到如下画面表示数据库初始化成功:
初始化postgreSQL (/app/postgres/postgresql)
#/app/postgres/postgresql/data数据文件的存放路径
------------------------------------------------------------------------------------
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
./pg_ctl -D /app/postgres/postgresql/data -l logfile start
[postgres@paas-test-postgres-001 bin]$ pwd
初始完数据库之后,data文件夹会出现一系列文件
drwx------. 5 postgres postgres 41 Mar 17 00:31 base
drwx------. 2 postgres postgres 4096 Mar 17 00:31 global
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_commit_ts
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_dynshmem
-rw-------. 1 postgres postgres 4513 Mar 17 00:31 pg_hba.conf
-rw-------. 1 postgres postgres 1636 Mar 17 00:31 pg_ident.conf
drwx------. 4 postgres postgres 68 Mar 17 00:31 pg_logical
drwx------. 4 postgres postgres 36 Mar 17 00:31 pg_multixact
drwx------. 2 postgres postgres 18 Mar 17 00:31 pg_notify
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_replslot
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_serial
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_snapshots
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_stat
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_stat_tmp
drwx------. 2 postgres postgres 18 Mar 17 00:31 pg_subtrans
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_tblspc
drwx------. 2 postgres postgres 6 Mar 17 00:31 pg_twophase
-rw-------. 1 postgres postgres 3 Mar 17 00:31 PG_VERSION
drwx------. 3 postgres postgres 60 Mar 17 00:31 pg_wal
drwx------. 2 postgres postgres 18 Mar 17 00:31 pg_xact
-rw-------. 1 postgres postgres 88 Mar 17 00:31 postgresql.auto.conf
-rw-------. 1 postgres postgres 23986 Mar 17 00:31 postgresql.conf
1、base目录是表空间目录;
2、global目录是相关全局变量目录;
3、pg_hba.conf是访问控制配置文件;
4、postgresql.conf是postgresql的主配置文件。
------------------------------------------------------------------------------------
八、设置参数文件(postgres)
1)修改pg_hba.conf文件
使用postgres用户,执行vi /app/postgres/postgresql/data/pg_hba.conff 修改如下参数
将该文件中的IPV4的连接修改为0.0.0.0/0,表示信任来自所有id连接的客户端,加密方式改为md5
host all all 0.0.0.0/0 md5
-------------------------------------------------------------------------------------------------------------------
METHOD指定如何处理客户端的认证。常用的有ident,md5,password,trust,reject
ident是Linux下PostgreSQL默认的local认证方式,凡是能正确登录服务器的操作系统用户(注:不是数据库用户)就能使用本用户映射的数据库用户不需密码登录数据库。用户映射文件为pg_ident.conf,这个文件记录着与操作系统用户匹配的数据库用户,如果某操作系统用户在本文件中没有映射用户,则默认的映射数据库用户与操作系统用户同名。比如,服务器上有名为user1的操作系统用户,同时数据库上也有同名的数据库用户,user1登录操作系统后可以直接输入psql,以user1数据库用户身份登录数据库且不需密码。很多初学者都会遇到psql -U username登录数据库却出现“username ident 认证失败”的错误,明明数据库用户已经createuser。原因就在于此,使用了ident认证方式,却没有同名的操作系统用户或没有相应的映射用户。解决方案:1、在pg_ident.conf中添加映射用户;2、改变认证方式。
md5是常用的密码认证方式,如果你不使用ident,最好使用md5。密码是以md5形式传送给数据库,较安全,且不需建立同名的操作系统用户。
password是以明文密码传送给数据库,建议不要在生产环境中使用。
trust是只要知道数据库用户名就不需要密码或ident就能登录,建议不要在生产环境中使用。
reject是拒绝认证。
在文件查找 listen_addresses,他的值说明
如果希望只能从本地计算机访问PostgreSQL数据库,就将该项设置为'localhost';
如果希望从局域网访问PostgreSQL数据库,就将该项设置为PostgreSQL数据库的局域网IP地址;
如果希望从互联网访问PostgreSQL数据库,就将该项设置为PostgreSQL数据库的互联网IP地址;
如果希望从任何地方都可以访问PostgreSQL数据库,就将该配置项设置为“*”;
-------------------------------------------------------------------------------------------------------------------
2)使用postgres用户,执行vi /app/postgres/postgresql/data/postgresql.conf 修改如下参数(监听地址、监听端口、最大连接数):
将localhost改为*,放开listen_addresses = '*',表示监听所有的网络连接。其他的参数保持默认即可 port = 5432 默认端口
listen_addresses = '*'
port = 5432
max_connections = 100
九、启动数据库(postgres)
pg_ctl -D /app/postgres/postgresql/data -l logfile start
其中-l代表日志文件目录,现在是当前目录,但是最好是新建一个log文件夹 指定pg_ctl -D /app/postgres/postgresql/data -l /app/postgres/postgresql/log start
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile start
waiting for server to start.... done
server started
[postgres@paas-test-postgres-001 bin]$ pwd
/app/postgres/postgresql/bin
--------------------------------------------------------
于是postgresql数据库启动了!我们首先可以通过postgresql客户端psql来进行连接,直接输入psql既可以进入:
使用psql客户端
postgres=# SELECT pg_reload_conf();
使用pg_ctl命令(PG内置命令)
pg_ctl reload
----------------------------------------------------------------------------------------------------------------------------
第一次登录
现在就是需要进行设置postgres用户的密码(默认为空),用psql连接成功后直接输入\password即提示输入两次密码,或者在psql命令提示符下输入如下命令:alter user postgres with password 'postgres';
开启远程登陆之后,要想在服务器控制台登陆只能这样psql -h 你服务器Ip -U postgres
[postgres@paas-test-postgres-001 bin]$ psql
psql (11.7)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 |
template0 | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \password
Enter new password:
Enter it again:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 |
template0 | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF8 | en_US.UTF8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=#
----------------------------------------------------------------------------------------------------------------------------
postgres=# quit 退出
十、停止数据库(postgres)
pg_ctl stop -D $PGDATA -m fast -W
pg_ctl stop -D /app/postgres/postgresql/data -m fast -W
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl stop -D /app/postgres/postgresql/data -m fast -W
server shutting down
[postgres@paas-test-postgres-001 bin]$
---
启动
# /home/postgres/PGDATA为数据文件存放路径,里面也有一些配置文件
pg_ctl -D /app/postgres/postgresql/data -l logfile start
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile start
waiting for server to start.... done
server started
[postgres@paas-test-postgres-001 bin]$
---
停止
pg_ctl -D /app/postgres/postgresql/data -l logfile stop
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile stop
waiting for server to shut down.... done
server stopped
[postgres@paas-test-postgres-001 bin]$
---
重启
pg_ctl -D /app/postgres/postgresql/data -l logfile restart
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile restart
waiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started
[postgres@paas-test-postgres-001 bin]$
---
查看状态
pg_ctl -D /app/postgres/postgresql/data -l logfile status
---
[postgres@paas-test-postgres-001 bin]$ pg_ctl -D /app/postgres/postgresql/data -l logfile status
pg_ctl: server is running (PID: 38326)
/app/postgres/postgresql/bin/postgres "-D" "/app/postgres/postgresql/data"
---
11、配置系统服务,开机自启动(root)
进入postgresql源码包的解压目录(本文为/root/postgresql-11.7),执行命令 cp contrib/start-scripts/linux /etc/init.d/postgresql,然后 vim /etc/init.d/postgresql,进行如下配置修改
-----
# contrib/start-scripts/linux
## EDIT FROM HERE
# Installation prefix
prefix=/app/postgres/postgresql
# Data directory
PGDATA="/app/postgres/postgresql/data"
# Who to run the postmaster as, usually "postgres". (NOT"root")
PGUSER=postgres
# Where to keep a log file
PGLOG="$prefix/log/serverlog"
-----
然后再分别执行如下命令:
chmod u+x /etc/init.d/postgresql
chkconfig --add postgresql
chkconfig --list postgresql
---------------
远程连接
远程连接这里使用的是Navicat for PostgreSQL
--注意这一段:tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 38898/postgres
------------------------------------------------------------------------------------------------------------------------
ps -ef | grep -i post
[postgres@paas-test-postgres-001 bin]$ netstat -anlp | grep 5432
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 38898/postgres
tcp 0 0 192.168.3.21:5432 192.168.3.1:60644 ESTABLISHED 38908/postgres: pos
tcp6 0 0 :::5432 :::* LISTEN 38898/postgres
unix 2 [ ACC ] STREAM LISTENING 60774 38898/postgres /tmp/.s.PGSQL.5432
------------------------------------------------------------------------------------------------------------------------
基本操作:
创建db、创建用户、分配权限
CREATE DATABASE testdb;
CREATE USER testuser CREATEDB LOGIN PASSWORD 'dbpasswd';
GRANT ALL ON DATABASE testdb TO testuser;
#删除db、删除用户
drop database testdb;
drop role testuser;
---------------------------------------------
其他相关命令
在数据库命令行执行
\l 查看所有数据库
\c db 来进入db数据库
\d 查看数据库表
创建数据库database
CREATE DATABASE Tdatabase;
创建用户auser
CREATE USER auser CREATEDB LOGIN PASSWORD 'Upasswd';
将Tdatabase所有权限赋给用户auser
GRANT ALL ON DATABASE Tdatabase TO auser;
grant all privileges on database 数据库名称 to dbuser;
删除数据库Tdatabase
drop database database;
删除用户auser
drop role auser;
插入数据
insert into 表名 values(..,..);
创建表
create table "数据库名"."表名" (
.....
);
----------------------------------------