method one:
1、Install therepository RPM:
yum installhttps://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat10-10-2.noarch.rpm
2、Install the clientpackages:
yum install postgresql10
3、Optionally installthe server packages:
yum install postgresql10-server
4、Optionallyinitialize the database and enable automatic start:
/usr/pgsql-10/bin/postgresql-10-setupinitdb
systemctl enable postgresql-10
systemctl start postgresql-10
5、FOR RHEL / CENTOS /SL / OL 5,6
service postgresql initdb
chkconfig postgresql on
6、FOR RHEL / CENTOS /SL / OL 7 OR FEDORA 24 AND LATER DERIVED DISTRIBUTIONS:
postgresql-setup initdb
systemctl enable postgresql.service
systemctl start postgresql.service
Configured to use
1. change password
#yum PostgreSQL installation, the default will be built called "Postgres" account system for performing the PostgreSQL;
[psql_master the root @ ~] # SU - Postgres
after a user # handover, is changed to the prompt " PG-1 @ Postgres: / var / lib / pgSQL # ";
# at the same time the database will generate a file named" postgres "database user, and password is automatically generated;
#PostgreSQL Log-free under the system user account database with the same name dense;
postgres@pg-1:/var/lib/pgsql# psql -U postgres
# Change password after entering the database;
Postgres Postgres = # ALTER User password with '@ 123 Postgres'
root@pg-1:/root# systemctl start postgresql-10
root@pg-1:/root# su - postgres
postgres@pg-1:/var/lib/pgsql# psql -U postgres
psql (10)
Type "help" for help.
postgres=# alter user postgres with password 'm2018'
postgres-#
2. Allow remote access
# configuration file, the default can only access the native PostgreSQL;
# modify listen_addresses = 'localhost' as listen_addresses = '*', allows all remote access;
# modify configuration files need to restart the service.
[root @ psql_master ~] # sed -i "s | #listen_addresses = 'localhost' | listen_addresses = '*' | g" / opt / postgresql-10/10 / data / d
or
installation directory
/ opt / postgresql- 10/10 / data / data / pg_hba.conf add the following
listen_addresses = '*'
3. The host authentication
# 82 after the first row, the "IPv4 local connections" to allow new clients;
# indicates a host type, a first "host" "all" representative of DB, the second "all" for user, "172.29.3.67/32" on behalf of client ip, "trust" Representative authentication mode;
# authentication mode except "trust", there are "peer", "ident", "md5", "password" , and specifically refer pg -hba file: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html
# pg.hba modify files need to restart the service.
[root @ psql_master ~] # vim /opt/postgresql-10/10/data/data/pg_hba.conf
Host All All 172.29.3.67/32 Trust
4. Set the environment variable
[psql_master the root @ ~] # Vim / etc / Profile
Export the PATH = $ the PATH: / usr / pgSQL-10 / bin
[psql_master the root @ ~] # Source / etc / Profile
The Restart Service
[root @ psql_master ~] # systemctl restart postgresql-10
Iptables 6. The
#postgresql tcp5432 default on port
[psql_master the root @ ~] # Vim / etc / sysconfig / iptables
-A State --state the INPUT NEW -m -m -p TCP TCP --dport 5432 -j ACCEPT
[root@psql_master ~]# service iptables restart
four. Using a verification
1. Viewing Port
[root @ psql_master ~] # netstat -tunlp
2. Use simple
1) to create a user
postgres = # create user postuser1 with password 'user1 @ 123';
2) Create a database
# specify the database owner
postgres = # create database postdb1 owner postuser1 ;
3) database empower
# account not only empower the login console
postgres = # grant all privileges on database postdb1 to postuser1;
4) Log in New Database
# account to log in using the new operating system layer of the new database, the login prompt is "postdb1 =>"; #
if used directly in the postgres account "postgres = # \ c postdb1; " logged in, login user is still Postgres,
-bash-4.2 $ psql -U postuser1 -h 127.0.0.1 -p 5432 -d postdb1
5)创建表
postdb1=> create table tb1(
id int primary key,
name VARCHAR(20),
salary real
);
6) Insert data
postdb1 => INSERT INTO TB1 (
ID, name, the salary)
values (
101, 'Mike', 5000.00);
7) query
postdb1 => select * from tb1;
Method Two:
1, download the source code and extracting
https://www.postgresql.org/download/linux/redhat/
wgethttps://ftp.postgresql.org/pub/source/v9.4.15/postgresql-9.4.15.tar.gz
tar -xvzf postgresql-10.0.tar.gz
./configure
sudo make
sudo make install
2, create user groups and users
groupadd postgres
useradd -g postgres postgres
passwd postgres
3. Create a data directory
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
chmod700 /usr/local/pgsql/data
4, database operations
/ Usr / local / pgsql / bin / initdb -D / usr / local / pgsql / data # initialize the database
/ Pg_ctl start \ stop \ restart -D / usr / local / pgsql / data / # Start \ Stop \ Restart the database
/ Usr / local / pgsql / bin / postgres -D / usr / local / pgsql / data> logfile 2> & 1 & # Set the log output position
/ Usr / local / pgsql / bin / createdb test # create a test database
/ Usr / local / pgsql / bin / psql test # start testing database
5, modify postgresql.conf
listen_addresses = '*'
port= 5432
6, modify pg_hba.conf
# "local" is for Unix domainsocket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
7, remote connection
1) Check the firewall is turned off: firewall-cmd --state
2) to start the service: systemctl start firewalld.service
3) Close the service: systemctl stop firewalld.service
4) restart the service: systemctl restart firewalld.service
5) displays the service status: systemctl status firewalld.service
6) Enable service at boot: systemctl enable firewalld.service
7) disable the service at boot: systemctl disable firewalld.service
8) Check whether the service start-up: systemctl is-enabled firewalld.service; echo $?
9) has started to view the list of service: systemctl list-unit-files | grep enabled
10) was added to develop port: firewall-cmd --zone = public --add-port = 5432 / tcp --permanent
11) reload the firewall: firewall-cmd --reload