Linux安装PostgreSQL

1、登录到服务器,这里我用的是ubuntu

root@vultr:/wgcloudTest# sudo apt-get install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-image-4.15.0-156-generic linux-modules-4.15.0-156-generic linux-modules-extra-4.15.0-156-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libpq5 postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common ssl-cert sysstat
Suggested packages:
  postgresql-doc locales-all postgresql-doc-10 libjson-perl openssl-blacklist isag
The following NEW packages will be installed:
  libpq5 postgresql postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common ssl-cert sysstat
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,327 kB of archives.
After this operation, 20.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpq5 amd64 10.21-0ubuntu0.18.04.1 [108 kB]
Get:2 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 postgresql-client-common all 190ubuntu0.1 [29.6 kB]
Get:3 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 postgresql-client-10 amd64 10.21-0ubuntu0.18.04.1 [943 kB]
Get:4 http://jp.clouds.archive.ubuntu.com/ubuntu bionic/main amd64 ssl-cert all 1.0.39 [17.0 kB]
Get:5 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 postgresql-common all 190ubuntu0.1 [157 kB]
Get:6 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 postgresql-10 amd64 10.21-0ubuntu0.18.04.1 [3,771 kB]
Get:7 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 postgresql all 10+190ubuntu0.1 [5,884 B]
Get:8 http://jp.clouds.archive.ubuntu.com/ubuntu bionic-updates/main amd64 sysstat amd64 11.6.1-1ubuntu0.1 [295 kB]
......
postgres@vultr:~$ su - postgres
postgres@vultr:~$ psql
psql (12.11 (Ubuntu 12.11-0ubuntu0.20.04.1))
Type "help" for help.

postgres=# ALTER USER postgres WITH PASSWORD '123456';
ALTER ROLE
postgres@vultr:~$

	
OK,创建完成了,注意最后我们把用户postgres的密码改成了123456
postgresql,启动、重启、停止命令,分别如下

root@vultr:~# service postgresql start
root@vultr:~# service postgresql restart
root@vultr:~# service postgresql start
	
2、配置postgres远程访问,可选操作。如果postgresql和server在同一个主机上,就不用看此步骤了
修改postgresql.conf文件
vi /etc/postgresql/12/main/postgresql.conf
去掉 #listen_addresses = 'localhost'  前面#号
并改为 listen_addresses = '*'
修改pg_hba.conf文件
vi /etc/postgresql/12/main/pg_hba.conf

在文档末尾加上以下内容
host     all     all      0.0.0.0/0      md5 
重启服务
service postgresql restart