Postgresql安装

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:

1. 安装软件

yum安装

_
选择版本之后,按照语句安装。
这里的安装是安装到/usr/pgsql-10下的。
数据目录是在:/var/lib/pgsql/10/data/

源码安装(后续内容对应源码安装)

首先下载tar.gz源码文件:postgresql-10.1.tar.gz

#解压文件 
tar -xzvf postgresql-10.1.tar.gz
#将解压缩后的文件拷贝到/usr/local下
mv postgresql-10.1 /usr/local
#进入路径
cd /usr/local
ln -s postgresql-10.1 pgsql
cd pgsql
./configure
make
su
make install

注:编译时的报错及解决:

1.configure: error: no acceptable C compiler found in $PATH
原因:没有安装C编译器,yum安装一下:
[root@tingtingcent pgsql]# yum install gcc*
2:configure: error: readline library not found

yum search readline
yum install readline-devel.x86_64 

3.configure: error: zlib library not found

yum search zlib
yum install zlib-devel.x86_64

2.创建用户,初始化数据库

[root@tingtingcent pgsql]# groupadd postgres
[root@tingtingcent pgsql]# useradd -g postgres -m postgres
[root@tingtingcent pgsql]# mkdir -p /data/pgsql/data
[root@tingtingcent pgsql]# chown postgres /data/pgsql/data

[root@tingtingcent pgsql]# su - postgres
[postgres@tingtingcent ~]$ /usr/local/pgsql/bin/initdb -D /data/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
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:

    /usr/local/pgsql/bin/pg_ctl -D /data/pgsql/data -l logfile start

3.启动数据库服务

[postgres@tingtingcent ~]$ /usr/local/pgsql/bin/pg_ctl -D /data/pgsql/data -l pgsql-error.logfile start
waiting for server to start.... done
server started

看一下启动的日志:

[postgres@tingtingcent ~]$ tail -200f pgsql-error.logfile 
2017-12-13 21:42:49.253 EST [47314] LOG:  listening on IPv6 address "::1", port 5432
2017-12-13 21:42:49.253 EST [47314] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2017-12-13 21:42:49.272 EST [47314] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2017-12-13 21:42:49.312 EST [47315] LOG:  database system was shut down at 2017-12-13 21:39:43 EST
2017-12-13 21:42:49.316 EST [47314] LOG:  database system is ready to accept connections

pg的默认端口是5432.

4.创建自己的数据库,测试连接功能使用等

[postgres@tingtingcent ~]$ /usr/local/pgsql/bin/createdb test
[postgres@tingtingcent ~]$ /usr/local/pgsql/bin/psql test
psql (10.1)
Type "help" for help.

test=# \db
       List of tablespaces
    Name    |  Owner   | Location 
------------+----------+----------
 pg_default | postgres | 
 pg_global  | postgres | 
(2 rows)

test=# 

5.添加环境变量

 为了可以直接执行psql等命令,需要添加环境变量,编辑~/.bash_profile文件。

_
编辑完成后可执行source命令或者退出重新登录即可用。

为了可以使用man文档,添加以下内容到shell启动文件里:
MANPATH=/usr/local/pgsql/man:$MANPATH
export MANPATH

6.设置账户,更改密码等。

  进入数据目录下查看配置文件。

[postgres@tingtingcent ~]$ cd /data/pgsql/data
[postgres@tingtingcent data]$ ls -l | grep conf
-rw-------. 1 postgres postgres  4513 Dec 13 21:39 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Dec 13 21:39 pg_ident.conf
-rw-------. 1 postgres postgres    88 Dec 13 21:39 postgresql.auto.conf
-rw-------. 1 postgres postgres 22778 Dec 13 21:39 postgresql.conf
  • pg_hba.conf:客户端认证的配置文件,相当于mysql数据库的mysql.user表的host限制,用户名和密码的对应还是在数据库中进行的,配置应用访问的用户,复制账户都在这里配置。
  • pg_ident.conf:用户名映射表:当使用像Ident 或 GSSAPI这样的外部认证系统时,发起连接的操作系统用户名可能与他需要连接的数据库用户是不同的。 在这种情况下,用户名映射可用于映射操作系统用户名到数据库用户。要使用用户名映射,在pg_hba.conf 中的选项中指定map=map-name。这个选项支持所有接受外部用户名的认证方法。 因为不同的映射可能需要不同的连接,使用的映射名在pg_hba.conf中的map-name 参数中指定,表示为每个独立的连接使用哪个映射。用户名映射在身份映射文件中定义,缺省名为pg_ident.conf 。
  • postgresql.conf:参数配置文件,相当于mysql中的my.cnf。

此处,配置一下:
在pg_hba.conf中加入下面的行:

_
这一行允许名称为tangyuan的用户可以从192.168.225.0这个子网中的任意机器连接到数据库test。
创建用户:

postgres=# create role tangyuan
login
valid until '2018-06-30 00:00:01'
password 'tangyuan32';
CREATE ROLE

用户授权

postgres=# grant all privileges on database test to tangyuan;
GRANT

远程连接测试:

[root@a18tangyuan data]# psql -h 192.168.225.180 -p 5432 -U tangyuan
psql: 无法联接到服务器: 没有到主机的路由
    服务器是否在主机 "192.168.225.180" 上运行并且准备接受在端口5432 上的 TCP/IP 联接?

这里需要修改配置文件postgresql.conf,监听远程连接:
_
更改后重启,还是报这个错误,则检查防火墙、iptables等的设置,更改后连接:

[root@a18tangyuan data]# psql -h 192.168.225.180 -p 5432 -U tangyuan -d test
用户 tangyuan 的口令:
psql (10.1)
输入 "help" 来获取帮助信息.

test=> 

参考文档:官网源码安装

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
9月前
|
关系型数据库 数据库 数据安全/隐私保护
PostgreSQL安装和使用教程
PostgreSQL安装和使用教程
333 0
|
11月前
|
存储 关系型数据库 数据库
Windows安装Postgresql之解压版
Windows安装Postgresql之解压版
380 0
|
关系型数据库 数据安全/隐私保护 PostgreSQL
Windows环境下安装PostgreSQL
Windows环境下安装PostgreSQL
263 0
|
9月前
|
关系型数据库 Linux PostgreSQL
Linux centos8 docker中安装postgresql12.4及远程访问设置
Linux centos8 docker中安装postgresql12.4及远程访问设置
512 0
|
2月前
|
安全 关系型数据库 数据库
PostGreSQL安装压缩包格式
PostGreSQL安装压缩包格式
|
2月前
|
SQL 缓存 关系型数据库
postgresql的安装和使用
postgresql的安装和使用
83 0
|
15天前
|
安全 关系型数据库 Linux
|
2月前
|
关系型数据库 PostgreSQL
postgresql安装
postgresql安装
62 8
|
2月前
|
SQL 关系型数据库 数据库
如何在 Debian 12 上安装 PostgreSQL?
【4月更文挑战第25天】
254 0
|
2月前
|
关系型数据库 Linux 网络安全
postgresql 出现连接不上问题(或者安装完连接不上)附加安装教程 亲测有效!
postgresql 出现连接不上问题(或者安装完连接不上)附加安装教程 亲测有效!
42 0