How to deploy pgsql on a linux server, the security version and possible solutions to various problems (nanny-level tutorial)


Prepare

提示:市面上那些在linux服务器部署pgsql好多都是水货,效果良莠不齐,笔者花了两天时间成功部署了pgsql,记录下方便自己以后部署,也方便其他有需要的码农

Server environment:

CentOS Linux release 7.9.2009 (Core)

postgresql version

postgresql-15.0

Do not download through the mirror. There are tens of thousands of roads, and safety is the first. The mirror source is not necessarily safe. Just go to the official website to download honestly. The link to the official website is as follows: https://www.postgresql.org/ftp /source/
Click to download the latest version of the suffix format as .tar.gzthe compressed package file.
In my case, the download is under the v-15.0 filepostgresql-15.0.tar.gz


1. Installation

1. Upload the compressed package to the server;

What kind of management tool do you use for this step? Just use it to upload it. I won’t elaborate too much. The author uses winscp to upload.
The best place to upload is the server package management location, usually /usr/local/srcbelow, which is the conventional package management location.

2. Decompress the compressed package;

What you need to pay attention to in this step is that due to user permission issues, you may report an error when decompressing. Remember that the user who logs in to the service must be the root user, and then
use tar -zxvf ./postgresql-15.0.tar.gzthe decompressed compressed package. Another problem encountered here is that carelessly did not enter the src directory
so The overall steps are

cd /usr/local/src
tar -zxvf ./postgresql-15.0.tar.gz

3. Observe whether there is a postgresql-15.0 file generated in the src directory. If it is generated, it means that the decompression is successful. If not, please carefully follow the above steps. There are two ways to observe, one is to enter commands in the src directory on the linux terminal lsto view, and the other is to view with management tools. Like me, it is to view on winscp. The first method is recommended
insert image description here

This is the effect picture under my server. You can see that there are not only compressed packages but also decompressed files in the src directory;

4. View the download instruction file;

Enter the postgresql-15.0 file, the command is

cd postgresql-15.0
ls

Check the files in the directory, you can see an INSTALL file with the naked eye,

vim INSTALL

Check the file, this step is very important, this file is the installation steps given by the official website, you
insert image description here
can see that there are installation instructions at the bottom, we just follow it honestly, copy those commands, and the instruction to exit this page is to
press Click esc, then enter :q!, which means exit without saving

5. Create an administrative user, that is, create a user on the linux system. Note that what I said is to create an administrative user on linux. Use the following commands to create
sudo useradd+username
sudo passwd+password
. Our words are to create a postgres user to manage this pgsql database,
then That is, sudo useradd postgres
the password should also be set, just in case,
if the password is forgotten after the setting is completed, delete the user command

 userdel -r 用户名

Verify whether you have set the command
cut -d: -f1 /etc/passwd
to view the users under your server, if you can find postgres, it means you have created this user;

6. Create a pgsql folder;

Create a pgsql folder in the /usr/local directory to store PostgreSQL.

7. Jump back to the /usr/local/src/postgresql-15.0 directory again and run the ./configure file. Why run this file is because it
can be used to detect the system environment (for example, to detect whether there is a suitable compiler , whether there is a function library required by this software, etc.).
After the detection, the configure program will also help us create a GNUmakefile or makefile, a script file that can help us automate many compilation steps.
The command is as follows

cd /usr/local/src/postgresql-15.0
./configure --prefix=/usr/local/pgsql 

The purpose is to specify that PostgreSQL is installed in /usr/local/pgsql. You can see that it is the steps in INSTALL.

8. Check the files in the /usr/local/src/postgresql-15.0 directory again

ls

It can be found that there are some more files, which are compiled by executing ./configure, and the most important thing is that there is a GNUmakefile!
insert image description here
You can see that my words are the third from the bottom in the first row.
With this, it will be easy to handle.

8. Use the make command to compile;

as follows:

make clean;make

Wait for a while, the compilation time may be a little long, until it shows a prompt to leave the directory, some versions will also show a prompt that the installation is successful, these may be in English or Chinese, depending on the pgsql you installed.

9. Check whether the installation is successful;

Open /usr/local/pgsql to check, you can find that something is already installed, the command is as follows:

cd /usr/local/pgsql
ls

10. Establish user management;

Create a data folder under the /usr/local/pgsql directory;

mkdir data

Grant the management data permission to the previously created postgres user

chown postgres /usr/local/pgsql/data

2. Configure environment variables

1. Switch user

The command is as follows:

su - postgres

Mentioning root to switch users does not require a password, and ordinary users need a password to switch users. The switching command is

su - 用户名

2. Modify the configuration file

The command is as follows:

vi .bashrc

Modify the file to join
insert image description here

that's it

PGHOME=/usr/local/pgsql
export PGHOME
PGDATA=$PGHOME/data
export PGDATA
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH

Press insert to enter, press esc after writing, and then enter :wqto save and exit

Then exit and enter

source  .bashrc

means to revalidate the configuration file


3. Establish a database

Or execute it under the postgres user! ! !
1. Input command

initdb

Initialize the database

2.
The command to start the database is

pg_ctl start

Also attach other database directives

重启:pg_ctl restart
关闭:pg_ctl stop

3. Set the password of the postgres user;
here refers to setting the password of the database super user postgres.
The command is as follows:

psql -U postgres
\password
\q

4. Set up monitoring

1. Modify the pg_hba.conf configuration file under /usr/local/pgsql/data, and change the line under IPv4 local connections to the following: The effect is the same)
insert image description here
the command to open pg_hba.conf is

vi /usr/local/pgsql/data

Just open it under the postgres user.
Find the content of the screenshot and modify it to look like the red line.
2. Modify the postgresql.conf configuration file under /usr/local/pgsql/data and modify it as follows:
insert image description here
here is an error in the network diagram, click me In this way, change
listen_address = '*'
port = 5432
to open port 5432
and listen to all addresses at the same
time. It is about the 60th line. If I remember correctly, if it is successful, please give feedback and make progress together.
3.
The command to restart the database is the above. It's been said, it's this

pg_ctl restart

Finally, there are still some things to say. It is recommended not to use the default port 5432. It is easy to be scanned and cracked by brute force. In addition, the user password of the management database should not be the same as that of the user in the database, otherwise it will be easy to be cracked. The password must be complex. .

Summarize

This overall has been completed, and then if you want to connect to the database externally, remember to open the corresponding port, that is, port 5432, and then a column xxx does not exist error may be reported when nav12 or other connections are used. The solution to this is to use the latest nav16 connection, please go to the official website to download.
Attached is the mandatory deletion of the database.
If you don’t know how to operate the database
, you can also contact me. I will definitely help when I have time. Please correct me if there are any shortcomings.

Guess you like

Origin blog.csdn.net/weixin_51759592/article/details/127357054