Postgresql basic introduction and deployment

As an IT technician, I am no stranger to databases. I have directly used mysql and sqlserver databases, and have also systematically studied oracle databases. As for postgresql databases, I have only heard of them in the gis field and foreign companies, so I am interested.
Learn by sorting out relevant online learning materials, and actually build and deploy postgresql in the local construction environment and briefly use it. Record it here for continuous and in-depth learning in the future, and also provide reference for latecomers. There are inevitably omissions in the article. Hope Readers' corrections are greatly appreciated!

1. Basic introduction

1.1 Basic Information

PostgreSQL is a powerful open source database system. After more than 15 years of active development and continuous improvement, PostgreSQL has gained a high reputation in the industry in terms of reliability, stability, and data consistency. Currently PostgreSQL can run on all major operating systems, including Linux, Unix and Windows.
PostgreSQL is a complete transaction security database that supports rich data types (such as JSON and JSONB types, array types) and custom types. The PostgreSQL database provides a rich interface, which can easily expand its functions. For example, you can implement your own index type under the GiST framework, support using C language to write custom functions and triggers, and also support using popular programming languages ​​to write custom functions. Define the function. PL/Perl provides the function of writing custom functions in Perl language, and of course PL/Python, PL/Java, PL/Tcl, etc.

As an enterprise-level database, PostgreSQL is proud of its various advanced features, such as multi-version concurrency control (MVCC), point-in-time recovery (PITR), table space, asynchronous replication, nested transactions, online hot standby , planning and optimization of complex queries, and pre-write logs for fault tolerance. It supports international character sets, multi-byte encodings, and supports operations such as sorting, case handling, and formatting in local languages. It also has complete high scalability for the large amount of data that can be managed and the concurrent access time allowed by a large number of users.

1.2 Advantages

PostgreSQL database has the following advantages:
PostgreSQL database is currently the most powerful open source database, it is the query language closest to the industry standard SQL92, and at least realizes 160 of the 179 main functions required in the SQL:2011 standard (Note: No database management system currently fully implements all major features of the SQL:2011 standard).
Stable and reliable: PostgreSQL is the only open source database that can achieve zero data loss. It is currently reported that some banks at home and abroad use the PostgreSQL database.
Open source saves money: The PostgreSQL database is open source and free, and uses a BSD-like protocol, so there are basically no restrictions on use and secondary development.
Wide support: PostgreSQL database supports a large number of mainstream development languages, including C, C++, Perl, Python, Java, Tcl, and PHP.
The PostgreSQL community is active: PostgreSQL basically releases a patch version every 3 months, which means that known bugs will be fixed soon, and the needs of application scenarios will be responded in a timely manner.

2. Compare mysql and oracle

2.1 Postgresql VS Oracle

Oracle database is currently the most powerful commercial database, and PostgreSQL is the most powerful open source database.
The difference between PostgreSQL and Oracle is that PostgreSQL has more functions to support Internet features. For example, the PostgreSQL data type supports network address type, XML type, JSON type, UUID type, and array type, and has powerful regular expression functions. For example, regular expression matching can be used in the where condition, and it can also be written in languages ​​such as Python and Perl. Stored procedures, etc. Plus, PostgreSQL is smaller. PostgreSQL can run perfectly on a machine with a small memory, such as a 512MB cloud host, while the Oracle database can basically run on a few gigabytes of cloud host. The Oracle installation package is often more than several GB in size, while the PostgreSQL installation package is only tens of MB in size. PostgreSQL can be easily installed in any environment. Oracle database installation takes hours, while PostgreSQL can be installed in minutes.

2.2 Postgresql VS Mysql

Both Postgresql and Mysql are open source databases.
PostgreSQL has the following advantages.
Powerful functions: support all mainstream multi-table join query methods, such as "Hash JOIN", "Sort Merge JOIN", etc.; field types also support array types, and even some business functions no longer need to be written to implement, just use them directly The functionality of the database does the trick.
Rich performance optimization tools and measurement information: There are a large number of performance views in the PostgreSQL database, which can easily locate problems (for example, you can see the SQL being executed, you can see who is waiting and which record is locked through the lock view, etc.) . PostgreSQL has designed a special architecture and process to collect performance data, including statistics on physical I/O, and performance data on table scans and index scans.
Good online operation function: when PostgreSQL adds a null column, it essentially just defines the column on the system table, without updating the physical structure, which allows PostgreSQL to complete the addition of a column in an instant. PostgreSQL also supports the function of online index creation, and the update operation can be unlocked during the index creation process.
Starting from PostgreSQL9.1, it supports synchronous replication (synchronous replication) function, and the high availability solution with zero data loss can be realized through the replication between Master and Slave.
It is convenient to write plug-ins to extend the functions of the PostgreSQL database: to support new functions of the mobile Internet, such as spatial indexes.
If the data access of the application is very simple, it is also very appropriate to use MySQL for the backend. But if the application is complex and you don't want to consume too much development resources, then PostgreSQL is a very wise choice.

3. Installation and deployment

3.1 Installation environment

服务器环境:centos7
postgresql版本:postgresql-14.0

3.2 Installation steps

  1. Download the installation package
    insert image description here
  2. Unzip and compile
yum -y install gcc gcc-c++ readline-devel zlib-devel
tar -zvxf postgresql-14.0.tar.gz
cd postgresql-14.0
./configure
make && make install
/usr/local/目录下会生产pgsql目录
  1. users and permissions
useradd postgres
chown -R postgres:postgres pgsql/
  1. Initialize the database
进入bin目录
su postgres
./initdb /usr/local/pgsql/data
  1. start up
./pg_ctl -D /usr/local/pgsql/data -l logfile start
#开启
/home/postgres/pgsql/bin/pg_ctl  -D /home/postgres/data/ -l logfile start
#查看状态
/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/data/ -l logfile status
#停止
/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/data/ -l logfile stop

insert image description here
insert image description here

  1. Modify the configuration
    After initialization, you can see the generated data directory data and related data and configuration files in the postgresql directory, pg_hba.conf and postgresql.conf, one
    is the access control configuration (127.0.0.1 is changed to the trusted client ip The network segment enables remote access),
    one is the postgresql main configuration file (listen_address=localhost is changed to an asterisk to make it listen to the entire network)

Configure the authentication methods allowed by the server in pg_hba.conf, and add the following line.

TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 password

Postgresql.conf Edit or add the following line, so that PostgreSQL can accept connection requests from any IP, and open the port, which was originally commented

listen_addresses = '*'
port = 5432
  1. restart pgsql
./pg_ctl -D /usr/local/pgsql/data/ -l logfile restart

insert image description here

  1. Login and change password
./psql
ALTER USER postgres WITH PASSWORD '123456';
psql -d postgres -p 5432 -U postgres -h Localhost
  1. Navicat remote connection is successful
    insert image description here

4. Boot up automatically

  1. Find the startup script under the installation package
postgresql-14.0/contrib/start-scripts/linux
  1. copy startup script
cp postgresql-14.0/contrib/start-scripts/linux /etc/init.d/postgresql
  1. Modify script configuration
# Installation prefix
prefix=/usr/local/pgsql
# Data directory
PGDATA="/usr/local/pgsql/data"
# Who to run the postmaster as, usually "postgres".  (NOT "root")
PGUSER=postgres
# Where to keep a log file
PGLOG="$PGDATA/serverlog"
  1. Increase execution permissions
chmod a+x /etc/init.d/postgresql
  1. Register to start automatically
chkconfig命令将该脚本注册为开机启动便可:
chkconfig --add postgresql

restart verification

ps -ef|grep postgresql

insert image description here
insert image description here

5. pgadmin client

https://www.pgadmin.org/download/

insert image description here

6. References

https://www.postgresql.org/
https://www.pgadmin.org/
https://blog.51cto.com/u_15289334/3321664
https://blog.csdn.net/Victory_Lei/article/details/121414749

Guess you like

Origin blog.csdn.net/shy871/article/details/125234837