首先默认安装软件(本文以PostgreSQL 11.1为例,其他版本类似)。
sudo apt install postgresql-11
等待软件自动安装并完成配置,启动服务。
服务状态如下:
vmware@vmware-virtual-machine:~$ service postgresql status
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2018-12-13 17:16:01 CST; 24min ago
Main PID: 19530 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 2289)
CGroup: /system.slice/postgresql.service
12月 13 17:16:01 vmware-virtual-machine systemd[1]: Starting PostgreSQL RDBMS...
12月 13 17:16:01 vmware-virtual-machine systemd[1]: Started PostgreSQL RDBMS.
vmware@vmware-virtual-machine:~$
安装完成后,默认条件下:
(1)创建 "postgres" Linux 用户
(2)创建 "postgres" 未知密码默认数据库管理员账号
(3)创建 "postgres" 数据库
部分默认配置文件夹如下
#配置文件
vmware@vmware-virtual-machine:~$ ls /etc/postgresql/11/main
conf.d environment pg_ctl.conf pg_hba.conf pg_ident.conf postgresql.conf start.conf
#数据文件
vmware@vmware-virtual-machine:~$ sudo ls /var/lib/postgresql/11/main/
base pg_commit_ts pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf postmaster.pid
global pg_dynshmem pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postmaster.opts
vmware@vmware-virtual-machine:~$
登录PostgreSQL 数据库(两种方法)。
#方法1
vmware@vmware-virtual-machine:~$ sudo -u postgres psql
psql (11.1 (Ubuntu 11.1-1.pgdg18.04+1))
输入 "help" 来获取帮助信息.
postgres=# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)
postgres=# \q
#方法2
vmware@vmware-virtual-machine:~$ sudo -i
root@vmware-virtual-machine:~# su - postgres
postgres@vmware-virtual-machine:~$ psql
psql (11.1 (Ubuntu 11.1-1.pgdg18.04+1))
输入 "help" 来获取帮助信息.
postgres=#
数据库默认本机可以访问,默认监听本机端口。现在修改数据库内网访问限制。(本文增加192.168.0.1/16网段;0.0.0.0代表所有主机,不推荐)
#开启监听
53 #------------------------------------------------------------------------------
54 # CONNECTIONS AND AUTHENTICATION
55 #------------------------------------------------------------------------------
56
57 # - Connection Settings -
58 listen_addresses='*'
#主机连接信息
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 192.168.0.1/16 md5
修改完毕配置文件以后记得reload配置文件。
root@vmware-virtual-machine:~# su - postgres
postgres@vmware-virtual-machine:~$ psql
psql (11.1 (Ubuntu 11.1-1.pgdg18.04+1))
输入 "help" 来获取帮助信息.
postgres=#
postgres=# SELECT pg_reload_conf();
pg_reload_conf
----------------
t
(1 行记录)
postgres=#
需要注意,自动安装数据库管理员密码未知,需要手动修改。修改数据库密码为123456。
root@vmware-virtual-machine:~# su - postgres
postgres@vmware-virtual-machine:~$ psql
psql (11.1 (Ubuntu 11.1-1.pgdg18.04+1))
输入 "help" 来获取帮助信息.
postgres=# ALTER USER postgres WITH PASSWORD '123456';
ALTER ROLE
postgres=#
测试数据库连接成功。(本文以Microsoft Visual Studio 安装好数据库驱动为例。)