01-PostgreSQL installation and remote connection

1. Windows installation PostgreSQL

①:Download

1. Official website download

Address: https://www.postgresql.org/download/

  • Choose the corresponding system

image.png

  • Click to download

image.png

  • Select the version to download (I downloaded version 14.X here)

image.png

  • Download completed

image.png

2. Network disk download

Link: https://pan.baidu.com/s/1u3ZcgWb6Woh_0n0ZV2PG9A?pwd=Coke

Extraction code: Coke

②: Installation

1. You can first create two folders to store the installation directory and data respectively

image.png

2. Just double-click to run

image.png

3.next

image.png

4. Select the installation directory and then next

image.png

5. Select components, select all by default, then next

image.png

6. Select the data storage path and then next

image.png

7. Set a password for the database super user postgres, then next

image.png

8. Set the port, the default is enough, and then next

image.png

9. Select the locale, the default is enough, and then next

image.png

10. Install the above settings, then next

image.png

  1. Install

image.png

image.png

12. After the installation is complete, check the box to start the stack generator and click finish

image.png

13. The stack generator installation interface pops up, select the previously installed software (the computer must be connected to the Internet), and click next

image.png

14. The installation application interface pops up, select the language pack to install, and click next

image.png

15. Set the installation path and then next step

image.png

16. Click Next (uncheck, skip installation)

image.png

17. The installation language option pops up, click OK

image.png

18. Click next

image.png

19. Install next

image.png

image.png

image.png

image.png

③: Test connection

1. Using Navicat

image.png

image.png

2. Use idea

image.png

④: Set up remote connection

image.png

2. Install PostgreSQL on Ubuntu

①: Download command

1. Official website: https://www.postgresql.org/download/

image.png

image.png

②: Execute download command

1. Install the specified version of PostgreSQL

sudo apt install postgresql-14

image.png

image.png

③: Modify the default database password

  1. PostgreSQL will create a postgres user as the administrator account by default, and the password is random
  2. Use sudo -u postgres psql to log in to the database. This method does not require a password
  3. Enter alter user postgres with password 123456’; after the postgres=# prompt appears.
  4. Change the password. After the operation is successful, enter \q to exit psql

1. Use sudo -u postgres psql to log in to the database

2. Change password: alter user postgres with password 123456’;

image.png

3. \q exit psql

image.png

④: Log in with new password

Logged in with new password psql -U postgres -d postgres -h 127.0.0.1 -p 5432

image.png

⑤: Set to allow remote access

1. Modify the listening address

Command: sudo vim /etc/postgresql/14/main/postgresql.conf

Remove the comment from #listen_addresses = 'localhost' and change it to listen_addresses = '*'

image.png

2. Modify accessible IP segments

  • Tips: Modify file permissions: sudo chmod -R 777 *

Command: sudo vim /etc/postgresql/14/main/pg_hba.conf

Add a line at the following location to allow remote access from any address through password

image.png

3. Restart the database

sudo service postgresql restart

image.png

4. Navicat test connection

image.png

3. Installing PostgreSQL in CentOS

①: Download command

1. View system architecture uname -m

image.png

2. Official website: https://www.postgresql.org/download/

image.png

image.png

image.png

# Install repository RPM:
yum install -y https://download.postgresql.org/pub/repos/yum/reporrpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL
yum install -y postgresql14-server

# Optionally initialize the database and enable automatic startup:
/usr/pgsql-14/bin/postgresql-14-setup initdb
systemctl enable postgresql-14
systemctl start postgresql-14

②: Start installation

1. Install repository RPM

yum install -y https://download.postgresql.org/pub/repos/yum/reporrpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

image.png

2. Install PostgreSQL

yum install -y postgresql14-server

image.png

image.png

3. You can choose to initialize the database and enable automatic startup

/usr/pgsql-14/bin/postgresql-14-setup initdb
systemctl enable postgresql-14
systemctl start postgresql-14

image.png

③:Set password

1. Switch to postgres user

su-postgres

image.png

2. Enter psql

image.png

3. Modify the postgres superuser password

alter user postgres with password 'new password';

image.png

4. Use exit to exit

image.png

④: Set up remote permission connection

1. Modify the postgresql.conf file

Enable remote access and change #listen_addresses = 'localhost' to listen_addresses = '*'

vim /var/lib/pgsql/14/data/postgresql.conf

image.png

2. Modify the pg_hba.conf file

Add a line host all all 0.0.0.1/0 scram-sha-256

image.png

3. Restart the database service

systemctl restart postgresql-14

image.png

4. Log in with new password

psql -U postgres -h 127.0.0.1

image.png

5. Use Navicat to test the connection

image.png