1.下载安装包
从Postgres官方网站下载postgresql-9.6.1-1-Linux-x64-binaries.tar.gz安装包
2.创建用户
创建Postgres用户:
- 1
- 1
3.解压
为了保证我们使用postgres用户安装完成后其他用户也能使用,我们采用root用户解压安装包到/usr目录中,再将相应目录的权限改回postgres
- 1
- 2
- 3
- 1
- 2
- 3
4.更改目录权限
切换到/usr目录,找到刚才解压的pgsql文件夹,将pasql文件夹的所有者改回postgres
- 1
- 1
5.建立数据目录
在/opt目录下建立postgres的数据目录,并更改文件夹所有者为postgres
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
如果后期忘记了posgresql安装到什么目录了,可以通过查找pg_hba.conf,来定位postgresql的位置
添加PG_HOME和PGDATA环境变量
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
6.初始化数据库
切换到postgres用户,初始化数据库
- 1
- 2
- 3
- 1
- 2
- 3
执行完成后结果如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
如果想对postgres进行配置,可以编辑如下文件:
- 1
- 2
- 1
- 2
7.启动数据库
先创建日志目录
- 1
- 2
- 1
- 2
启动数据库:
- 1
- 1
查看服务是否启动成功
- 1
- 1
当看到类似下面的结果时,说明服务启动成功了
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
调用如下命令查看postgres服务:
- 1
- 1
然后发现并没有结果,因此需要将postgresql注册到服务列表
8.修改postgres用户的访问密码并测试建库建表
PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为’postgres’
直接在控制台输入以下命令:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
可以看到我们刚才插入的那条数据
9.将postgresql-9.6添加到服务列表
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
添加以下内容:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
需要根据安装路径修改配置中相应的路径信息
接着就可以使用service postgresql-9.6 start/stop/status来操作postgresql的启停了
10.把postgresql加入自启动列表
- 1
- 2
- 1
- 2
查看一下自启动列表
- 1
- 1
在这里可以看到postgresq-9.6已经在其中了。
11.配置postgresql允许远程访问
只需要修改data目录下的pg_hba.conf和postgresql.conf这两个文件:
pg_hba.conf:配置对数据库的访问权限;
postgresql.conf:配置PostgreSQL数据库服务器的相应的参数
11.1修改pg_hba.conf
- 1
- 1
在IPV4中添加下面那一行内容
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
重新加载postgresql配置文件(可选):
- 1
- 2
- 1
- 2
11.2修改postgresql.conf
- 1
- 1
定位到listen_addresses,并将localhost改为*
- 1
- 1
注:修改完配置后需要重新启动postgresql远程连接才能生效
至此,postgresql的安装和配置已经全部完成