This article adopts the RPM package offline installation method to install Postgresql version 9.6.
Table of contents
Step 1: Installation package preparation
Step 2: Install the RPM package
Commonly used Postgresql commands:
One-click restart postgresql script
Error 1: Installing the rpm package lacks dependencies
Error 2: su postgres shows the problem of bash-4.2$
Error 3: s.PGSQL.5432.lock" does not exist
Error 4: The error link cannot be accessed because the firewall is not closed! ! !
报错5:Postgresql FATAL: could not create semaphores: No space left on device
Detailed explanation of PostgreSQL's pg_hba.conf configuration parameters
Step 1: Installation package preparation
Official website download
RepoView: PostgreSQL PGDG 9.6 Updates RPMs
I downloaded the rpm package:
postgresql9.6.zip-PostgreSQL Documentation Resources-CSDN Download
Step 2: Install the RPM package
create postgres user
useradd postgres
passwd postgres
Install the rpm package
rpm -ivh postgresql96-*
The Postgres installer creates folders under /var and /usr
- /var/lib/pgsql: used to store the default data folder of the Postgres database
- /usr/pgsql-9.6: used to store commands for the Postgres database, relying on information such as libraries and document directories
- /var/lib/pgsql/9.6/data: The user stores the pg_hba.conf and postgresql.conf configuration files of PG default configuration
In order to simplify the later operation and maintenance, add postgresql to the system PATH, and test whether the postgres command returns, and test whether the installation is successful
Create a folder:
mkdir -p /var/lib/pgsql/{data,xlog_archive}
chown -R postgres:postgres /var/lib/pgsql/
chmod 0700 /var/lib/pgsql/data
Edit environment variables
vi /etc/profile
# Add the following statement at the end of the file
export PGDATA=/var/lib/pgsql/data
export PATH=$PATH:/usr/pgsql-9.6/bin
make the environment variable take effect
source /etc/profile
postgres --version
Initialize the database
After the data is configured (if a custom path is configured), the database is not started by default. Initialization is required.
Method 1: sudo -u postgres /usr/pgsql-9.6/bin/initdb -D /data/postgres/ under the root user
Method 2: su - postgres
initdb -D /var/lib/pgsql/data
If you want to connect other servers to pg, you need to change the parameter settings:
Modify postgresql.conf
vi /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
Modify pg_hba.conf
vi /var/lib/pgsql/data/pg_hba.conf
Added: host all all 0.0.0.0/0 md5
Start the postgres database
are postgres
pg_ctl -D /var/lib/pgsql/data/start
The corresponding command to stop the pg database is:
pg_ctl -D /var/lib/pgsql/data/ stop
login pg
psql -h localhost -U postgres -d postgres -W
Detailed psql command :
- -h host, specify the IP address of the Postgres database to connect to
- -U username: Specify the username to connect to the database
- -d database: Specify the database name to connect to
- -p port: specify the service port of the database connection
- -w: Indicates that the user is not prompted for a password
- -W : Indicates to verify the database user password
- -l : Indicates to list the database information available to Postgres
Postgresql uninstallation
yum remove postgresql*
and then check to see if there are
rpm -qa|grep postgres
Commonly used Postgresql commands:
View all databases: =# \l or \list
List the tables of the current database: =# \d
Create a database: =# create database mydb;
Switch databases: =# \c mydb
Insert a table in the current database: =# create table test (id int, body varchar(100));
Create a new user: =# create user test with password 'test';
Modify the account password: =# alter user postgres with encrypted password 'mypass';
Grant the specified account to the specified database permission: = # grant all privileges on database mydb to test;
Remove the specified account to the specified database permission: =# revoke all privileges on database mydb to test;
One-click restart postgresql script
Restarting postgresql sometimes results in the following error 3: could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": No such file or directory, you can use this script command to restart pg with one click:
The script checks to restart if pg is running, and starts if pg is not running
sh start_postgresql.sh /data/XXX (your startup address)
common error
Error 1: Installing the rpm package lacks dependencies
Error error: Failed dependencies:
libxslt.so.1()(64bit) is needed by postgresql96-contrib-9.6.20-1PGDG.rhel6.x86_64
libxslt.so.1(LIBXML2_1.0.11)(64bit) is needed by postgresql96-contrib-9.6.20-1PGDG.rhel6.x86_64
libxslt.so.1(LIBXML2_1.0.18)(64bit) is needed by postgresql96-contrib-9.6.20-1PGDG.rhel6.x86_64
libxslt.so.1(LIBXML2_1.0.22)(64bit) is needed by postgresql96-contrib-9.6.20-1PGDG.rhel6.x86_64
solution:
Just install dependencies: yum install libxslt
Error 2: su postgres shows the problem of bash-4.2$
Delete the postgres user and recreate the user
userdel postgres
useradd postgres
Notice:
Remember to re-assign permissions after deleting and rebuilding the user
chown -R postgres:postgres /var/lib/pgsql/
Error 3: s.PGSQL.5432.lock" does not exist
FATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": No such file or directory
< 2021-02-06 08:27:44.357 EST > LOG: database system is shut down
Solution: just create a folder
mkdir -p /var/run/postgresql/
chown -R postgres:postgres /var/run/postgresql/
Error 4: The error link cannot be accessed because the firewall is not closed! ! !
systemctl status firewalld
报错5:Postgresql FATAL: could not create semaphores: No space left on device
Reference article link:
Postgresql FATAL: could not create semaphores: No space left on device_dazuiba008's Blog-CSDN Blog
For my own situation, modifying the above parameters is invalid. After killing all vnc processes, restarting pg is successful.
Detailed explanation of PostgreSQL's pg_hba.conf configuration parameters
Detailed explanation of PostgreSQL's pg_hba.conf configuration parameters - Programmer Sought
What to do if you forget your password, you can change the pg_hba.conf file:
Modify the pg_hba.conf file under the data directory
local all all md5=>change to trust
save and exit, then use pg_ctl reload to re-read the pg_hba.conf file to make the configuration take effect
Enter the postgresql database
alter user postgres with password 'the password you want to set ';
Then change the trust modified in pg_hba.conf back to the original md5