linux installation postgresql13
- Reference article: Ubuntu 18.04 Installing Postgresql 13 + Postgis and its related components + pgAdmin 4
1. Install postgresql13
1.1 Installation
- Linux download link: https://www.postgresql.org/download/linux/ubuntu/
- Installation command
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update sudo apt-get -y install postgresql-13 # -后面是自己的版本
1.2. Database initialization
-
Enter the default installation path and create a new folder
cd /usr/share/postgresql mkdir pgsql_data
-
Grant folder permissions
sudo chmod 777 /usr/share/postgresql/pgsql_data
-
Database initialization
/usr/lib/postgresql/13/bin/initdb -D /usr/share/postgresql/pgsql_data
-
Service start and stop
# 启动 /usr/lib/postgresql/13/bin/pg_ctl -D /usr/share/postgresql/pgsql_data start # 停止 /usr/lib/postgresql/13/bin/pg_ctl -D /usr/share/postgresql/pgsql_data stop
1.3. Configure remote access
1.3.1 Modify configuration file
- Modify
pg_hba.conf
filescd /usr/share/postgresql/pgsql_data # 这个路径修改,我的远程连接不了 cd /etc/postgresql/13/main/ # 这个路径修改,远程连接可以
- Edit the file pg_hba.conf
vim pg_hba.conf
- Add content [Add/modify: Allow any user to access the database with password from any machine, add the following line as the first rule:]
host all all 0.0.0.0/0 md5
- Edit the file pg_hba.conf
- Modify
postgresql.conf
filescd /usr/share/postgresql/pgsql_data # 这个路径修改,我的远程连接不了 cd /etc/postgresql/13/main/ # 这个路径修改,远程连接可以
- Edit the file postgresql.conf
vim postgresql.conf
- Modify content
listen_addresses = '*' # 原值为 listen_addresses = 'localhost'
- Edit the file postgresql.conf
1.3.2 Restart the service
- Restart service
sudo /etc/init.d/postgresql restart
- Check service status
service postgresql status
1.3.3 Test connection
- test connection
psql postgres://username:password@ip:5432/databasename # 主机ip 端口号默认5432 psql postgres://postgres:[email protected]:5432/testdb # 本地测试
1.4 Uninstall
sudo apt-get purge 'postgresql-15' # 卸载15版本
sudo apt-get autoremove 'postgresql-15'
2. Install Postgis
2.1 Installation
- Modify
sources.list
filessudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main" >> /etc/apt/sources.list'
- Solve by
vim
modifying/etc/apt/sources.list
vim /etc/apt/sources.list # 打开文件 deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main # 添加的内容
- Solve by
- Installation command
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add - # sudo apt update 报错执行。不报错不执行 sudo apt install postgis sudo apt install postgresql-13-pgrouting sudo apt install postgis-gui
2.2 Add postgis to the database
2.2.1 View files
- Enter the relevant directory
/usr/share/postgresql/13/contrib/ # 13为自己的版本
- Check if there
postgis.sql
arespatial_ref_sys.sql
files- If not uninstalled and reinstalled
postgis
- If not uninstalled and reinstalled
2.2.2 Add postgis to the database
- method one
- Database add
postgis
commandpsql -d mapdb1 -f /usr/share/postgresql/13/contrib/postgis-3.4/postgis.sql psql -d mapdb1 -f /usr/share/postgresql/13/contrib/postgis-3.4/spatial_ref_sys.sql
- Report an error
- Because PostgreSQL uses Peer authentication by default, that means the names of the database user and the operating system user must be the same. That is, create an operating system user in the database
- Because PostgreSQL uses Peer authentication by default, that means the names of the database user and the operating system user must be the same. That is, create an operating system user in the database
- Database add
- Method Two
- Create a spatially extended database
create database testdb; # 创建数据库 CREATE EXTENSION postgis; # 进入执行以下postgis扩展
- Create table [no error, indicating success]
CREATE TABLE IF NOT EXISTS geotable ( id integer NOT NULL, geom geometry(MultiPolygon) );
- Create a spatially extended database
2.2.3 Create operating system user
- Log in as postgres user
sudo su postgres
- Create user
CREATE ROLE user LOGIN; # user为自己的操作系统用户名
- Grant superuser privileges
ALTER USER user SUPERUSER;
- Database add
postgis
commandpsql -d testdb -f /usr/share/postgresql/13/contrib/postgis-3.4/postgis.sql psql -d testdb -f /usr/share/postgresql/13/contrib/postgis-3.4/spatial_ref_sys.sql
3. Install pgAdmin 4
- Windows comes with postgresql installed, but Ubuntu needs to install it yourself.
- Link: https://www.pgadmin.org/download/pgadmin-4-apt/
- Related commands
curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] http://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update' sudo apt install pgadmin4
3.1pgAdmin 4 remote connection database
- Create service group
- Create link
- Fill in the information
- Ready to connect