Postgresql database installation in linux

1. Download version

  pgsql has many types of packages. There are corresponding compiled packages for different linux distributions. The installation is very convenient. In addition, if you can compile source code installation or install official compiled binary packages for general linux platforms, the source package installation is only There is one more compilation step than binary installation, and the rest are the same, so the installation method used here is to install the compiled binary package

  pgsql official website address: https://www.postgresql.org/, after entering, click download to come to the download page, here click the Other Linux option under Linux, and then click tar.gz archive below to download the binary archive, I only introduce binary Installation method

  

2. Then you come to the final pgsql download page, the address is: https://www.enterprisedb.com/download-postgresql-binaries, download the latest version of 10.1 as shown below. Note what version of Linux is yours

  

3. Unzip the database and put it in the specified directory:

tar -xvzf postgresql-10.1-1-linux-x64-binaries.tar.gz

# After unzipping it, the directory is pgsql. Move the folder to your own installation directory. My installation directory is in /usr/local/pgsql/

mv pgsql/    /usr/local/pgsql/

4. Create a pgsql user and set a password:

useradd postgres 
passwd postgres 
# Enter the password twice and confirm

5. Note: The password set here is the login password of the linux user postgres, not the password of the pgsql server

  Create a pgsql data directory: mkdir /usr/local/pgsql/data 

  Authorize directory access permissions for postgres users: chown postgres /usr/local/pgsql/data 

  Then switch to the postgres user to operate the database, the pgsql database will use postgres as the default user, execute: su-postgres switch

6. Set the environment variables of pg, and then use the pg_ctl tool to start pg. Execute the following commands separately (operate under the postgres user)

PATH=$PATH:$HOME/.local/pgsql/bin:$HOME/pgsql/bin:/usr/local/pgsql/pgsql/bin

export PGDATA=/usr/local/pgsql/data

export PATH
7. Execute pg_ctl --help to see if this command is executed normally, as shown in the figure below, if not, please check the environment variable configuration

The start command and restart command are as shown in the screenshot above. pg_ctl start starts; pg_ctl stop stops; pg_ctl restart restarts;

8. Before starting, please confirm whether your data directory authorizes my data directory to be usr/local/pgsql/data/

chmod -R 0700 /usr/local/pgsql/data/

9. Start pg service pg_ctl start

Check whether the startup is successful.

10. Use psql to enter the database

 

Set the connection user password alter user pguser with password'new password';

11Set up remote login

The steps to configure a remote connection to the PostgreSQL database are very simple, just modify the pg_hba.conf and postgresql.conf in the data directory.

pg_hba.conf: Configure access permissions to the database,

postgresql.conf: Configure the corresponding parameters of the PostgreSQL database server.

The following describes the configuration steps:

 1. Modify the pg_hba.conf file to configure the user's access permissions (the line starting with # is the comment content):

# TYPE DATABASE  USER    CIDR-ADDRESS     METHOD
# "local" is for Unix domain socket connections only
local all    all               trust
# IPv4 local connections:
host  all    all    127.0.0.1/32     trust
host  all    all    192.168.1.0/24    md5

host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
Among them, Article 7 is a newly added content, which means that all hosts on the network segment 192.168.1.0 are allowed to use all legal databases The user name accesses the database and provides encrypted password authentication.

Among them, Article 8 is a newly added content, which means that all hosts are allowed to use all legal database user names to access the database and provide encrypted password verification.

Among them, the number 24 is the subnet mask, which means that computers from 192.168.1.0--192.168.1.255 are allowed to access!

 

2. Modify the postgresql.conf file to modify the monitoring mode of the database server to monitor connection requests from all hosts.

Locate #listen_addresses='localhost'. After the PostgreSQL installation is complete, the default is to only accept connection requests from the local localhost.

Remove the # at the beginning of the line and modify the content of the line to listen_addresses='*' to allow the database server to listen for connection requests from any host 3.

3. Test the connection

psql -h  ip -U postgres -d postgres -p 5432

12 After setting the remote connection password, set the firewall port pg port default is 5432

I turned off the firewall of the server directly. Because the server is an Alibaba Cloud server, I need to add a firewall whitelist in the Alibaba Cloud console.

End here

Guess you like

Origin blog.csdn.net/cainiaochen3/article/details/103603845
Recommended