centos7下postgresql數據庫安裝及配置


1、安裝

#yum install -y postgresql-server

2、postgresql數據庫初始化

#service postgresql initdb

3、啟動postgresql服務

#systemctl start postgresql
#systemctl enable postgresql

4、查看postgresql狀態

#netstat -tlunp|grep 5432
#ss -tlunp|grep 5432

5、連接postgresql數據庫
想連接到數據庫,需要切換到postgres用戶(默認情況下,安裝postgresql時會自動生成),使用psql連接到數據庫中,在該用戶下連接數據庫,是不需要密碼的。

#查看生成的用戶
# tail -1 /etc/passwd
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
#切換用戶,登錄postgresql
[root@centos7 data]# su - postgres
Last login: Thu Oct 24 16:06:34 CST 2019 on pts/1
-bash-4.2$ psql		#使用psql登錄數據庫
psql (9.2.24)
Type "help" for help.

postgres=# \l		#列出所有數據庫,相當於show databases;
                             List of databases
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     | 
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
(3 rows)
postgres=# \q		#退出數據庫
-bash-4.2$ 

6、postgresql的簡單配置
postgresql數據庫的配置主要是通過修改數據目錄下的postgresql.conf文件來實現的

# ll /var/lib/pgsql/data/  #yum安裝時路徑
total 48
drwx------. 5 postgres postgres    41 Oct 24 15:49 base
drwx------. 2 postgres postgres  4096 Oct 24 15:50 global
drwx------. 2 postgres postgres    18 Oct 24 15:49 pg_clog
-rw-------. 1 postgres postgres  4232 Oct 24 15:49 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Oct 24 15:49 pg_ident.conf
drwx------. 2 postgres postgres    32 Oct 24 15:49 pg_log
drwx------. 4 postgres postgres    36 Oct 24 15:49 pg_multixact
drwx------. 2 postgres postgres    18 Oct 24 15:50 pg_notify
drwx------. 2 postgres postgres     6 Oct 24 15:49 pg_serial
drwx------. 2 postgres postgres     6 Oct 24 15:49 pg_snapshots
drwx------. 2 postgres postgres    25 Oct 24 16:26 pg_stat_tmp
drwx------. 2 postgres postgres    18 Oct 24 15:49 pg_subtrans
drwx------. 2 postgres postgres     6 Oct 24 15:49 pg_tblspc
drwx------. 2 postgres postgres     6 Oct 24 15:49 pg_twophase
-rw-------. 1 postgres postgres     4 Oct 24 15:49 PG_VERSION
drwx------. 3 postgres postgres    60 Oct 24 15:49 pg_xlog
-rw-------. 1 postgres postgres 19805 Oct 24 15:49 postgresql.conf
-rw-------. 1 postgres postgres    57 Oct 24 15:50 postmaster.opts
-rw-------. 1 postgres postgres    92 Oct 24 15:50 postmaster.pid

7、修改監聽的Ip和端口

#vim /var/lib/pgsql/data/postgresql.conf
59 #listen_addresses = 'localhost'  # what IP address(es) to listen on;
 60                               # comma-separated list of addresses;
 61                         # defaults to 'localhost'; use '*' for all
 62                                         # (change requires restart)
 63 #port = 5432                            # (change requires restart)
 修改端口時,默認把前面#去掉,不修改的話默認為5432
 #修改完后重啟服務
 #systemctl restart postgresql

8、修改數據庫log相關的參數

# - Where to Log -

#log_destination = 'stderr'    # Valid values are combinations of
                               # stderr, csvlog, syslog, and eventlog,
                             # depending on platform.  csvlog
                              # requires logging_collector to be 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)

# 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
log_filename = 'postgresql-%a.log'      # log file name pattern,
                              # can include strftime() escapes
#log_file_mode = 0600                   # creation mode for log files,
                              # begin with 0 to use octal notation
log_truncate_on_rotation = on   # If on, an existing log file with 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
                             # happen after that much log output.
                              # 0 disables.

9、內存參數的設置

# - Memory -

shared_buffers = 32MB                   # min 128kB
#共享內存的大小,用於共享數據塊

10、添加用戶、創建數據庫、並登錄

#授權用戶遠程登錄
#vim /var/lib/pgsql/data/pg_hba.conf	#在最后一行添加
host	all		all		0.0.0.0/0 		md5
#vim /var/lib/pgsql/data/postgresql.conf
listen_addresses='*'		#把localhost更改為*
#systemctl restart postgresql
#創建用戶和密碼
postgres=# create user wang01 with password '123456';
CREATE ROLE
#創建數據庫,基於用戶wang01
postgres=# create database test01 owner wang01;
CREATE DATABE
#授權用戶
postgres=# grant all privileges on database test01 to wang01;
GRANT
#查看結果
postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     | 
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 test      | wang     | SQL_ASCII | C       | C     | =Tc/wang             +
           |          |           |         |       | wang=CTc/wang
 test01    | wang01   | SQL_ASCII | C       | C     | =Tc/wang01           +
           |          |           |         |       | wang01=CTc/wang01
 test02    | postgres | SQL_ASCII | C       | C     | 
(6 rows)

#登錄 
[root@centos7 data]# psql -h 10.0.0.3 -p 5432 -U wang -d test
Password for user wang: 
psql (9.2.24)
Type "help" for help.

test=> \l
                             List of databases
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     | 
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 test      | wang     | SQL_ASCII | C       | C     | =Tc/wang             +
           |          |           |         |       | wang=CTc/wang
(4 rows)

11、postgresql數據庫基本應用

1、列出所有數據庫
mysql: show databases
psql:    \l或\list
2、切換數據庫
mysql:  use dbname
psql:     \c dbname
3、列出當前數據庫下的所有表
mysql:  show tables
psql:       \d
4、列出指定表的所有字段
mysql:   shwo columns from table_name
psql:    \d table_name
5、查看表的基本情況
mysql:  describe table_name
psql:    \d+ table_name


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM