服务器:阿里云——经量应用服务器
Ubuntu版本号:Ubuntu 9.3.0-10ubuntu2
如果遇到连接超时问题,一定要留意端口是否开放!!!留意防火墙!!!
一、PostgreSQL
安装
- 查看 ubuntu提供的 PostgreSQL 版本:
apt-cache show postgresql
- 安装:
sudo apt-get install postgresql
- 或者:
sudo apt install postgresql postgresql-contrib
- 查看版本信息:
psql --version
- 安装postgrep数据库会默认创建一个用户
postgres
作为数据库的管理员,要实际使用 PostgreSQL,需要操作数据库切换到此用户:sudo su postgres
- 使用
psql
来进入 PostgreSQL Shell:进入后可以输入\q
以退出
配置
- 查询运行状态:
service postgresql status
- 更改用户密码:
ALTER USER postgres WITH PASSWORD 'my_password';
- 创建一个新用户:
CREATE USER my_user WITH PASSWORD 'my_password';
- 为
my_user
添加超级用户权限:ALTER USER my_user WITH SUPERUSER;
- 删除用户:
DROP USER my_user;
- 要切换其他用户登录,先
\q
退出,然后使用命令登录:psql -U my_user
修改linux系统postgres用户的密码
PostgreSQL会创建一个默认的linux用户postgres,修改该用户密码的方法如下:
-
删除用户postgres的密码:
sudo passwd -d postgres
-
设置用户postgres的密码:
sudo -u postgres passwd
相关命令
切换到postgres用户:su - postgres
连接数据库:psql
创建一个名为demo的数据库:createdb demo
删除数据库 demo (非PostgreSQL Shell状态): dropdb demo
进入demo数据库并进入其命令行:psql demo
检查日志信息:\conninfo
列出所有的数据库:\l
连接到指定的数据库:\c database_name
列出所有的表:\d
查看 用户: \du
获取帮助:\?
退出:\q
卸载
-
删除相关的安装
sudo apt-get --purge remove postgresql*
-
删除配置及文相关件
sudo rm -r /etc/postgresql/
sudo rm -r /etc/postgresql-common/
sudo rm -r /var/lib/postgresql/
-
删除用户和所在组
sudo userdel -r postgres
sudo groupdel postgres
问题
1.could not change directory to “/root”: Permission denied
解决方法:su - postgres
参考
[1] PostgreSQL command line cheatsheet
[2] 如何在 Ubuntu 上安装和配置 PostgreSQL
[3] How to Install and Configure PostgreSQL on Ubuntu
[4] Ubuntu16安装PostgreSQL
[5] Ubuntu PostgreSQL安装和配置
二、pgAdmin4
安装公钥
sudo curl
https://www.pgadmin.org/static/packages_pgadmin_org.pub
| sudo apt-key add
创建仓库配置文件
sudo sh -c 'echo "deb
https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$
(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
安装 pgAdmin4
桌面端+web端:sudo apt install pgadmin4
桌面端:sudo apt install pgadmin4-desktop
web端:sudo apt install pgadmin4-web
最后执行
sudo /usr/pgadmin4/bin/setup-web.sh
安装过程中,输入一个邮件地址和密码,作为超级管理员。
然后就可以使用了,访问这个地址来使用:http://127.0.0.1/pgadmin4
注意:如果你修改了端口,请补上你的端口号,比如:http://127.0.0.1:8099/pgadmin4
安装PostgreSQL后提供了createuser
和createdb
命令行程序。
参考
配置数据库以允许远程连接访问
安装完成后,默认只能本地才能连接数据库,其他机子访问不了,需要进行配置。
首先要确定PostgreSQL是否在您的系统上运行,之后确认端口:
默认情况下,PostgreSQL在端口5432上运行。netstat -pnltu | grep "5432"
修改监听地址
sudo gedit /etc/postgresql/9.5/main/postgresql.conf
将 #listen_addresses = ‘localhost’ 的注释去掉并改为 listen_addresses = ‘*’
修改可访问用户的IP段
sudo gedit /etc/postgresql/9.5/main/pg_hba.conf
在文件末尾添加:host all all 0.0.0.0 0.0.0.0 md5
,表示运行任何IP连接
重启数据库
sudo /etc/init.d/postgresql restart
三、PostGIS
安装
查看apt-get库中的软件版本支持 :sudo apt-cache search postgis
安装:sudo apt-get install postgis
配置
创建数据库:sudo -u postgres createdb postgis
为数据库添加PostGIS扩展插件:
sudo -u postgres psql -d gis -c 'CREATE EXTENSION postgis;' sudo -u postgres psql -d gis -c 'CREATE EXTENSION postgis_topology;'
参考
[1] Chapter 2. PostGIS Installation
[2] Ubuntu16.04 Postgresql & PostGIS在线安装部署