Install Postgresql and PostGIS on Ubuntu

1 Introduction

I have been doing GIS analysis recently, collecting the longitude and latitude points of the equipment to determine whether it enters and exits the fence and whether a road deviation alarm is generated. In the previous article, I introduced the use of C# under Windows. Reference article: < a i=1>Use Postgresql+Postgis for spatial geographical information analysis (road offset, entry and exit electronic fence, etc.)_postgres spatial analysis_Big Fish>'s blog-CSDN BlogRecently made a new one The Internet of Things project uses Springboot+mongoDB+MySQL, so consider using a Linux server to re-implement this service. This article only introduces the installation of Postgresql and PostGIS environment under Ubuntu.

2. Install Postgresql

2.1. Check the software version support in the apt-get library

Excuting an order:

sudo apt-cache search postgresql

I am using ubuntu 18.04 here. After checking, I found that postgresql-10 is currently supported.

2.2. Execute the installation command

sudo apt-get install postgresql-10

2.3.postgresql-common not configured yet. Error handling

After executing the installation command, it is found that postgresql-common cannot be installed successfully. The error message is: postgresql-common not configured yet.

 We check the /var/lib/dpkg/status file and find Package:postgresql-common

If its Status: install ok half-configured then change it to install ok installed

 Then execute the installation command. If the above situation is the case, the installation should be successful at this time.

3. Install postgis

Execute the installation script:

sudo apt-get install postgis

4. Modify the database default user name and password

The PostgreSQL database creates a postgres user as the database administrator with a random password, so the password needs to be changed here.

Execute the command to log in to PostgreSQL:

sudo -u postgres psql

Execute the command to change the login PostgreSQL password:

alter user postgres with password '你的密码';

After installing PostgreSQL, a Linux user will be created. This user must reset the password immediately, otherwise the server will easily be attacked. My server was previously installed with a mining program because the password was not changed, and it took a lot of effort. Only then did the mining program be completely cleared.

Execute the command to delete the original user password:

sudo  passwd -d postgres 

Set new password command:

sudo -u postgres passwd  

You will then be prompted to enter a new password:

Enter new UNIX password:

Confirm Password:

Retype new UNIX password: 

The final prompt is passwd: password updated successfully, which means the password has been changed.

5. Create database and add postgis

Switch Linux: postgres user

sudo su postgres  

Create a database and name it according to your needs

createdb postgis_24_sample

Add postgis support for database.

The script is in the /usr/share/postgresql directory by default, you can find it yourself

If the above directory files cannot be found, it means that the postgis installation failed or postgis was installed first and then postgresql, so uninstall the postgis service and reinstall it.​ 

Uninstall method:

sudo apt-get purge 'postgis*'
sudo apt-get autoremove 'postgis*

Then execute the sql script

psql -d postgis_24_sample -f /usr/share/postgresql/10/contrib/postgis-2.4/postgis.sql  
psql -d postgis_24_sample -f /usr/share/postgresql/10/contrib/postgis-2.4/spatial_ref_sys.sql    

If the following error is prompted during the process:
psql: could not connect to server: No such file or directory
 Is the server running locally and accepting
 connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

You can restart the postgres service and execute the restart command:

sudo service postgres restart

6. UsepgAdmin4 to connect to the database

We can use pgAdmin4 to connect to the postgresql database on the server. If the following error is reported, confirm whether the service has enabled remote connections.

Error message: could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "124.223.60.234" and accepting TCP/IP connections on port 5432?
Modify the configuration file postgresql.conf:

sudo vim /etc/postgresql/10/main/postgresql.conf

If listen_addresses is commented out, remove the # comment and change the value to *

Also modify pg_hba.conf

sudo vim /etc/postgresql/10/main/pg_hba.conf

 Add to file host all all 0.0.0.0/0 trust

 Finally logged in successfully

Considering security, it is recommended not to enable access to all external networks unless necessary. You can specify the corresponding IP or only allow access to the internal network, otherwise it will be easy to be hacked. As a result, postgresql is always restarted.

Guess you like

Origin blog.csdn.net/qq_17486399/article/details/131818888