1. Install PostgreSQL database on CentOS
Let's take the CentOS system as an example to explain the installation process of PostgreSQL.
1. Install postgresql using yum
[pengchengxiang@localhost ~]$ sudo yum install postgresql-server.x86_64 Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirror.bit.edu.cn * updates: mirror.bit.edu.cn Resolving Dependencies --> Running transaction check ---> Package postgresql-server.x86_64 0:8.4.20-3.el6_6 will be installed --> Processing Dependency: postgresql(x86-64) = 8.4.20-3.el6_6 for package: postgresql-server-8.4.20-3.el6_6.x86_64 --> Running transaction check ---> Package postgresql.x86_64 0:8.4.20-3.el6_6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: postgresql-server x86_64 8.4.20-3.el6_6 updates 3.4 M Installing for dependencies: postgresql x86_64 8.4.20-3.el6_6 updates 2.6 M Transaction Summary ================================================================================ Install 2 Package(s) Total download size: 6.0 M Installed size: 28 M Is this ok [y/N]: y Downloading Packages: (1/2): postgresql-8.4.20-3.el6_6.x86_64.rpm | 2.6 MB 00:02 (2/2): postgresql-server-8.4.20-3.el6_6.x86_64.rpm | 3.4 MB 00:06 -------------------------------------------------------------------------------- Total 680 kB/s | 6.0 MB 00:09 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : postgresql-8.4.20-3.el6_6.x86_64 1/2 Installing : postgresql-server-8.4.20-3.el6_6.x86_64 2/2 Verifying : postgresql-8.4.20-3.el6_6.x86_64 1/2 Verifying : postgresql-server-8.4.20-3.el6_6.x86_64 2/2 Installed: postgresql-server.x86_64 0:8.4.20-3.el6_6 Dependency Installed: postgresql.x86_64 0:8.4.20-3.el6_6 Complete!
2. Initialize the postgresql database
[pengchengxiang@localhost ~]$ sudo service postgresql initdb Initializing database: [ OK ]
2. Start the postgresql service
[pengchengxiang@localhost ~]$ sudo service postgresql start Starting postgresql service: [ OK ]
3. View the service status of postgresql
[pengchengxiang@localhost ~]$ sudo service postgresql status postmaster (pid 3496) is running...
Question: If you start the Qidong postgrepsql service before initializing the database, the following error will be reported:
[pengchengxiang@localhost ~]$ sudo service postgresql start /var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first. [FAILED]
2. Connect to the PostgreSQL database
If you want to connect to the database, you need to switch to the postgres user, and then use psql to connect to the database. Connecting to the database under this user does not require a password.
1. Switch the postgres user and connect to the database
[pengchengxiang@localhost ~]$ sudo su - postgres -bash-4.1$ psql psql (8.4.20) Type "help" for help. postgres=#
2. List all databases
postgres=# \l List of databases Name | Owner | Encoding | Collation | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres (3 rows)
3. Exit the database
postgres=# \q -bash-4.1$
3. PostgreSQL database directory
On a default installation, the PostgreSQL database directory is in the /var/lib/pgsql/data directory.
-bash-4.1 $ ls -l / var / lib / pgsql / data / total 80 drwx------. 5 postgres postgres 4096 Nov 16 23:55 base drwx------. 2 postgres postgres 4096 Nov 16 23:55 global drwx------. 2 postgres postgres 4096 Nov 16 23:55 pg_clog -rw-------. 1 postgres postgres 3411 Nov 16 23:55 pg_hba.conf -rw-------. 1 postgres postgres 1631 Nov 16 23:55 pg_ident.conf drwx------. 2 postgres postgres 4096 Nov 17 00:00 pg_log drwx------. 4 postgres postgres 4096 Nov 16 23:55 pg_multixact drwx------. 2 postgres postgres 4096 Nov 17 00:02 pg_stat_tmp drwx------. 2 postgres postgres 4096 Nov 16 23:55 pg_subtrans drwx------. 2 postgres postgres 4096 Nov 16 23:55 pg_tblspc drwx------. 2 postgres postgres 4096 Nov 16 23:55 pg_twophase -rw-------. 1 postgres postgres 4 Nov 16 23:55 PG_VERSION drwx------. 3 postgres postgres 4096 Nov 16 23:55 pg_xlog -rw-------. 1 postgres postgres 16886 Nov 16 23:55 postgresql.conf -rw-------. 1 postgres postgres 57 Nov 16 23:55 postmaster.opts -rw-------. 1 postgres postgres 45 Nov 16 23:55 postmaster.pid
4. Simple configuration of PostgreSQL
The configuration of the PostgreSQL database is mainly achieved by modifying the postgresql.conf file in the data directory.
1. Modify the listening ip and port
After connecting to the database using the postgresql user, go to the /var/lib/pgsql/data directory and edit the postgresql.conf file:
# - Connection Settings - #listen_addresses = '*' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost', '*' = all # (change requires restart) #port = 5432 # (change requires restart)
After modifying these two parameters, you need to restart to take effect
[pengchengxiang@localhost ~]$ sudo service postgresql restart Stopping postgresql service: [ OK ] Starting postgresql service: [ OK ]
After the ip and port are modified, the PostgreSQ client connection may also have the following problems: FATAL: no pg_hba.conf entry for host :
To solve this problem, just find /data/pg_hba.conf in the installation directory of PostgreSQL database, find "# IPv4 local connections:"
Add the IP of the machine requesting the connection under it
host all all 127.0.0.1/32 md5
32 is the network segment of the subnet mask; md5 is the password authentication method, which can be changed (see the description on the file pg_hba.conf)...
2. Modify the parameters related to the database log
Log collection, generally turned on
# This is used when logging to stderr: logging_collector = on # Enable capturing of stderr and csvlog # into log files. Required to be on for # csvlogs. # (change requires restart)
log directory, generally use the default value
# These are only used if logging_collector is on: log_directory = 'pg_log' # directory where log files are written, # can be absolute or relative to PGDATA
Keep only one day's log for circular coverage
log_filename = 'postgresql-%a.log' # log file name pattern, # can include strftime() escapes log_truncate_on_rotation = on # If on, an existing log file of the # same name as the new log file will be # truncated rather than appended to. # But such truncation only occurs on # time-driven rotation, not on restarts # or size-driven rotation. Default is # off, meaning append to existing files # in all cases. log_rotation_age = 1d # Automatic rotation of logfiles will # happen after that time. 0 disables. log_rotation_size = 0 # Automatic rotation of logfiles will
3. Configuration of memory parameters
共享内存的大小,用于共享数据块。如果你的机器上有足够的内存,可以把这个参数改的大一些,这样数据库就可以缓存更多的数据块,当读取数据时,就可以从共享内存中读,而不需要再从文件上去读取。
# - Memory - shared_buffers = 32MB # min 128kB # (change requires restart)
单个SQL执行时,排序、hash json所用的内存,SQL运行完后,内存就释放了。
# actively intend to use prepared transactions. #work_mem = 1MB # min 64kB
推荐教程:http://www.postgres.cn/docs/9.4/index.html