当前位置:网站首页>Installing postgresql11 database under centos7

Installing postgresql11 database under centos7

2022-07-07 07:48:00 Guardian stone

1. obtain PG Of Repo file

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

We can know through the following command repo The installation directory of the file :

[[email protected] ~]# rpm -ql pgdg-redhat-repo
/etc/pki/rpm-gpg
/etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
/etc/yum.repos.d/pgdg-redhat-all.repo

pgdg-redhat-all.repo File for yum Remote installation provides PostgreSQL Remote installation source for .

2. install PostgreSQL Client and server

yum install postgresql11 postgresql11-server

All the way y Key will do .

3. initialization

/usr/pgsql-11/bin/postgresql-11-setup initdb

And then postgresql Service join self start and start PostgreSQL

systemctl enable postgresql-11
systemctl start postgresql-11

4. permissions

installation is complete PostgreSQL After the client , Will be for Linux establish postgres Account , Let's set the password for this account first .

passwd postgres

New password:

Retype new password:

Input separately “ Your password ”

next , Linux from root Downgrade to postgres The user login shell terminal ( perhaps SSH), adopt pgsql Order to enter PG Console , Set up PG Super tube user postgres Password .

su postgres
psql
alter user postgres with password ' Your password ';
exit

5. modify PG Related configuration files , Set external access control rules

First configuration postgresql.conf file , to open up PG Network access port .

vi /var/lib/pgsql/11/data/postgresql.conf

modify listen_addresses The value of is ‘*’:

listen_addresses = '*'          # what IP address(es) to listen on;

Next configuration pg_hba.conf file , Fine grained control PG Service access and PG Master slave copied IP paragraph

vi /var/lib/pgsql/11/data/pg_hba.conf

Focus on the following configuration line. The end is “md5” Modification of configuration items :

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             192.168.83.0/16         md5
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            md5
host    replication     all             192.168.83.0/24         md5
host    replication     all             ::1/128                 ident

In the above configuration PostgreSQL Access to database services IP Configuration is 192.168.83.0/16 , The mask bit is 16, Then you can visit IP Segment is 192.168.0.1~192.168.255.254.

PostgreSQL Replication of services (replication) visit IP Configuration is  192.168.83.0/24, The mask bit is 24, Then you can visit IP Segment is 192.168.83.1~192.168.83.254.

notes : If you want to let go completely PostgreSQL Access control , Only need to 192.168.83.0/16 Change it to 0.0.0.0/0.

Finally, we need to return root user , restart PG service , Make the configuration work .

systemctl restart postgresql-11

6. Other configuration  

(1) Linux Downgrade login again postgres user , And then use pgsql Order to enter PG Console , Let's see. PostgreSQL Is it a Chinese environment .

select * from pg_database;

We focus on datcollate,datctype Two , See if it is zh_CN.UTF-8

postgres=# select * from pg_database;
  datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace |              
 datacl                
-----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------------
-----------------------
 postgres  |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | f             | t            |           -1 |         13880 |          561 |          1 |          1663 | 
 template1 |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t             | t            |           -1 |         13880 |          561 |          1 |          1663 | {=c/postgres,
postgres=CTc/postgres}
 template0 |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t             | f            |           -1 |         13880 |          561 |          1 |          1663 | {=c/postgres,
postgres=CTc/postgres}
(3 rows)

If not , perform SQL modify :

update pg_database set datcollate='zh_CN.UTF-8',datctype='zh_CN.UTF-8';

(2) Give Way postgres Users can reload pg_hba.conf The configuration file :

Return root user , Execute the following commands continuously :

echo 'export PGDATA=/var/lib/pgsql/11/data' >> /etc/profile
source /etc/profile

PGDATA The environment variables have been set globally .

We are demoted to postgres user , If to modify pg_hba.conf The configuration file , Direct command execution :

/usr/pgsql-11/bin/pg_ctl reload

  You can reload pg_hba.conf The configuration file , Without having to restart postgresql-11 service .

7.PostgreSQL A brief description of the command

(1). Create database , Be sure to specify roles (PG Mainly controlled by roles ).

for example , We first pass pgsql, Used postgres Super user login :

create role health login password ' Your password ';
create database health_db with owner= health;

We created roles “health”, Then create the database “health_db” When , You need to specify the owner of this database as “health”.

Corresponding delete database and role commands : 

drop database health_db;
drop role health;

When it comes to comprehensive operation “health_db” When it comes to databases , Please exit PG Console , Enter into Linux Of postgres User login status , Reuse “health” Character landing PG Console :

psql -U health -d health_db

We can use the following command :

\l  perhaps \l databaseName  view the database 
\d  perhaps  \d tableName  Look at the data sheet 
\du  To view the user 
\dnS  View mode 
\c databaseName  Switch database 
原网站

版权声明
本文为[Guardian stone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070416098907.html