linux installation postgresql13

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
    

    Insert image description here

  • 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.conffiles
    cd /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
      
      Insert image description here
  • Modify postgresql.conffiles
    cd /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'
      

1.3.2 Restart the service

  • Restart service
    sudo /etc/init.d/postgresql restart
    
  • Check service status
    service postgresql status
    
    Insert image description here

1.3.3 Test connection

  • test connection
    psql postgres://username:password@ip:5432/databasename
    # 主机ip 端口号默认5432
    psql postgres://postgres:[email protected]:5432/testdb  # 本地测试
    
    Insert image description here

1.4 Uninstall

sudo apt-get purge 'postgresql-15' # 卸载15版本
sudo apt-get autoremove 'postgresql-15'

2. Install Postgis

2.1 Installation

  • Modify sources.listfiles
    sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main" >> /etc/apt/sources.list'
    
    Insert image description here
    • Solve by vimmodifying/etc/apt/sources.list
      vim /etc/apt/sources.list  # 打开文件
      deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main # 添加的内容
      
  • 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 are spatial_ref_sys.sql files
    • If not uninstalled and reinstalledpostgis
      Insert image description here

2.2.2 Add postgis to the database

  • method one
    • Database add postgiscommand
      psql -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
        Insert image description here
  • 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)
      );
      

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 postgiscommand
    psql -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
    
    Insert image description here

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
    Insert image description here
  • Create link
    Insert image description here
  • Fill in the information
    Insert image description here
  • Ready to connect

Guess you like

Origin blog.csdn.net/m0_46926492/article/details/132756326