[关闭]
@zhangyy 2021-11-16T11:20:50.000000Z 字数 5320 阅读 148

PostgreSQL 基础

PostgreSQL系列


一: postgreSQL简介

image_1ekin24um11c41f5d1irvnjpb389.png-1513.9kB

image_1ekin7e0q128i43h7231gduer9m.png-1151.9kB

image_1ekin9rodmc51vm3a6n1on61d3t13.png-571.8kB

二:postgreSQL 在行业的应用

image_1ekinle6417rnkbms4j19sjkh11g.png-1308.4kB

三:postgreSQL的发展历史

image_1ekinrukt1qa134c1bh8ci7umq1t.png-888.9kB

image_1ekio3rda65vmqk10qk1hd5tbl2a.png-723.6kB

四:postgreSQL的版本的特性

image_1ekio5hs517kmm979331j8g1jaf2n.png-730.2kB

  1. postgreSQL 12 版本特性

image_1ekio8r441vormhq1poq1i3b1pfr34.png-1254.6kB

image_1ekiofe2s1tkn1gucsvd1ktkjpr3h.png-1735kB

五:postgreSQL 数据库 与其它数据的对比

image_1ekiohg3p1tg75j21becuuggpg3u.png-1495.1kB

image_1ekioopg1fq71m201gql1bjillp4b.png-1517.6kB

image_1ekiosuid1uv2oqn18s1tas1nas58.png-1433.7kB

image_1ekiov2f323cq0717qih28cm55l.png-1939.1kB

六:postgreSQL 的 体系结构

image_1ekip8ton1f6cqve1a1l1ojm1r862.png-373.6kB

image_1ekipamna165dhq91gau93d1s3m6f.png-803.4kB

image_1ekipf1crdid13l814ce11i2aml6s.png-1038.7kB

image_1ekipgjd715mg1rvuof1b3ocml79.png-1104.1kB

image_1ekipj4lj84217ca1f5a1vmg1ltk7m.png-1989.2kB

image_1ekipoobbd1513871lu813ls1u1483.png-1756kB

image_1ekiptllj135f195vam61cj71ng58g.png-756.2kB

image_1ekipvaoo1p4bpm3pbkrmqqe8t.png-1315.6kB

image_1ekiq1mk6d6bodp18g31k4q1qlt9a.png-1056.4kB

七: postgresql的 逻辑结构

image_1ekiq47td8m14on11rhp1s17oa9n.png-1041.9kB

八: postgreSQL 的存储结构

image_1ekiq89jfdi71sa51no1189r1r91a4.png-643.9kB

image_1ekiqb4h31omii5d1hls9pjho5ah.png-797.9kB

image_1ekiqflvpm311vo01s5t1aua15blau.png-1359.3kB

image_1ekiqnidd1sr0jeo1he41rgf1oa7bb.png-1261.5kB

image_1ekiqsdrng7g1sgii077sr2jbbo.png-703.9kB

image_1ekiqtpcouu56271uke1igu1620c5.png-1168.5kB

image_1ekiqvitnnarhhbh9dnc8g0ci.png-1552.5kB

image_1ekir20ap14v7ds51a24enc1tjcv.png-863.7kB

image_1ekir4tq1cae15t01t2f10m25bdc.png-563.5kB

image_1ekj1o0bb1bqqhvd1pgjq3117nq7c.png-400.3kB

  1. create user flyfish with password '123.com' nocreatedb;
  2. create database flyfishdb with owner=flyfish;

image_1ekj2un7q1d88obu1k2v1p0h98e8j.png-66.6kB

  1. psql.exe -h 192.168.100.90 -p 5432 -U flyfish -d flyfishdb
  2. create table flyfishtable(name varchar(50));
  3. insert into flyfishtable values('haha');
  4. commit ;
  5. select * from flyfishtable

image_1ekj322jk1nmp14062ih1f3e3gq9.png-63.1kB

image_1ekj32s9i183j1jvh1cgv10nlt27m.png-26.6kB

image_1ekj39qhj12ia1lcbc1j1ol91rid13.png-89.1kB

九:postgresql 的数据库表:

image_1ekir64hg3idjsr16k91sm34lrdp.png-628.8kB

十:postgresql 的安装部署:

10.1 postgresql 在windows 下面的安装

  1. windows server 2019x64 准备
  2. 下载 postgresql
  3. https://www.enterprisedb.com/download-postgresql-binaries

image_1ekivnkeo1g601m9j1gauj4ijit9.png-708.1kB

image_1ekivo1o511vb1epg1d03199me98m.png-448.8kB

image_1ekivoebm87j174d1mt51s349ej13.png-756.4kB

image_1ekivoq65g4q6il182b1bv5197c1g.png-907.1kB

image_1ekivpld113amgbc4do1vof1vdu1t.png-653.3kB

image_1ekivq2krhli84s1fjq10uhh532a.png-276.1kB

image_1ekivtoen1viv1983hdj4ig1kcc2n.png-584.3kB

image_1ekivu45kka6l5d390uocam434.png-299.8kB

image_1ekj060ko54b5dt2mt7nhd4b3h.png-469.6kB

image_1ekj0ue3t1f0gs4lke314m3u273u.png-154.9kB

image_1ekj14f7u1nnec641icddg0qrj4b.png-93.4kB

image_1ekj1chga1hakcqi1bs57di17m665.png-132.9kB

image_1ekj1bl3ovk11nnq15561hop1kgg5o.png-31.2kB

image_1ekj1jf9qk8ql3dsckn6hrbr6i.png-139.9kB

image_1ekj1kkekdqd1vte1hj81to6hcn6v.png-120.6kB


10.2 postgresql 在Linux 下面的安装

  1. 环境准备:
  2. echo "export LANG=en_US.UTF8" >> ~/.bash_profile
  3. cat ~/.bash_profile
  4. source ~/.bash_profile
  5. mount /dev/cdrom /mnt
  6. cd /etc/yum.repos.d
  7. mkdir bk
  8. mv *.repo bk/
  9. echo "[EL7]" >> itpux.repo
  10. echo "name = linux 7.6 dvd" >> itpux.repo
  11. echo "baseurl=file://mnt" >> itpux.repo
  12. echo "gpgcheck=0" >> itpux.repo
  13. echo "enabled=1" >> itpux.repo
  14. yum list
  15. systemctl set-default multi-user.target
  16. systemctl get-default
  17. echo "vm.swappiness=10" >> /etc/sysctl.conf
  18. echo "fs.aio-max-nr = 1048576" >> /etc/sysctl.conf
  19. echo "fs.file-max = 6815744" >> /etc/sysctl.conf
  20. echo "net.ipv4.ip_local_port_range = 9000 65500" >> /etc/sysctl.conf
  21. echo "net.core.rmem_default = 262144" >> /etc/sysctl.conf
  22. echo "net.core.rmem_max = 4194304" >> /etc/sysctl.conf
  23. echo "net.core.wmem_default = 262144" >> /etc/sysctl.conf
  24. echo "net.core.wmem_max = 1048586" >> /etc/sysctl.conf
  25. echo "kernel.shmmax = 1288490188" >> /etc/sysctl.conf
  26. echo "kernel.shmall = 314572" >> /etc/sysctl.conf
  27. echo "kernel.shmmni = 4096" >> /etc/sysctl.conf
  28. echo "kernel.sem = 4096 2147483647 2147483646 512000" >> /etc/sysctl.conf
  29. sysctl -p
  30. echo "* - nproc unlimited" > /etc/security/limits.d/90-nproc.conf
  31. echo "SELINUX=disable" >/etc/selinux/config
  32. echo "#SELINUXTYPE=trageted" >> /etc/selinux/config
  33. cat /etc/selinux/config
  34. setenforce 0
  35. systemctl status firewalld.service
  36. systemctl stop firewalld.service
  37. systemctl disable firewalld.service
  38. echo "session required pam_limits.so" >> /etc/pam.d/login
  39. cat /etc/pam.d/login
  40. echo "* soft nproc unlimited" >>
  41. /etc/security/limits.conf
  42. echo "* hard nproc unlimited" >>
  43. /etc/security/limits.conf
  44. echo "* soft nofile 16384" >>
  45. /etc/security/limits.conf
  46. echo "* hard nofile 65536" >>
  47. /etc/security/limits.conf
  48. echo "* soft stack unlimited" >> /etc/security/limits.conf
  49. echo "* hard stack unlimited" >>
  50. /etc/security/limits.conf
  51. cat /etc/security/limits.conf
  52. rm -f /etc/security/limits.d/*20*
  53. */
  54. vi /etc/default/grub
  55. GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet numa=off
  56. transparent_hugepage=never"
  57. grub2-mkconfig -o /etc/grub2.cfg
  58. numastat
  59. numactl --show
  60. numactl --hardware
  61. shutdown -r now
  62. rm -f /etc/udev/rules.d/70-persistent-net.rules
  63. hostname itpuxpg61
  64. echo "itpuxpg61" > /etc/hostname
  65. vi /etc/sysconfig/network-scripts/ifcfg-ens33
  66. IP、删 UUID
  67. reboot

  1. 安装postgreSQL
  2. 安装依赖包
  3. yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel perl python36 tcl openssl ncurses-devel openldap pam

image_1ekj3ni57oar830h091em5tub1g.png-280.3kB

  1. 创建 PostgreSQL 用户
  2. groupadd -g 60000 pgsql
  3. useradd -u 60000 -g pgsql pgsql
  4. echo "pgsql" |passwd --stdin pgsql
  5. 创建安装目录
  6. mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg12,soft}
  7. chown -R pgsql:pgsql /postgresql
  8. chmod -R 775 /postgresql

  1. 编译postgresql
  2. cd /postgresql/soft
  3. tar -zxvf postgresql-12.2.tar.gz
  4. cd postgresql-12.2
  5. ./configure --prefix=/postgresql/pg12 --without-readline (表示不显示历史)
  6. make
  7. make install

  1. su - pgsql
  2. vim /root/.bash_profile
  3. ---
  4. export LANG=en_US.UTF8
  5. export PS1="[`whoami`@`hostname`:"'$PWD]$'
  6. export PGPORT=5432
  7. export PGDATA=/postgresql/pgdata
  8. export PGHOME=/postgresql/pgl2
  9. export
  10. LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/l
  11. ib:/usr/local/lib:$LD_LIBRARY_PATH
  12. export PATH=$PGHOME/bin:$PATH:.
  13. export DATE=`date +"%Y%m%d%H%M"`
  14. export MANPATH=$PGHOME/share/man:$MANPATH
  15. export PGHOST=$PGDATA
  16. export PGUSER=postgres
  17. export PGDATABASE=postgres
  18. ---
  19. source /etc/profile

  1. 初始化
  2. su - pgsql
  3. /postgresql/pg12/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
  4. 可有这个命令启动:
  5. /postgresql/pg12/bin/pg_ctl -D /postgresql/pgdata -l logfile start

image_1ekj5ai2iksv1t0tbv5tce1aku1t.png-162.4kB

image_1ekj5m1d51oo31fkq11od43k1rnv2a.png-82.7kB


  1. cd /postgresql/pgdata/
  2. vim postgresql.conf
  3. ----
  4. listen_addresses = '*'
  5. port = 5432
  6. max_connections = 500
  7. logging_collector = on
  8. log_directory = 'pg_log'
  9. log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
  10. log_truncate_on_rotation = on
  11. shared_buffers = 1024MB
  12. max_wal_size = 1GB
  13. min_wal_size = 80MB
  14. ----
  15. vim pg_hba.conf
  16. ---
  17. host all all 0.0.0.0/0 md5
  18. ----

  1. cd /postgresql/pg12/bin/
  2. ./pg_ctl start 启动
  3. ./pg_ctl stop 停止
  4. /postgresql/pg12/bin/pg_ctl -D /postgresql/pgdata -l logfile start 启动
  5. /postgresql/pg12/bin/pg_ctl -D /postgresql/pgdata -l logfile stop 停止
  6. ---
  7. vi /etc/systemd/system/postgresql.service
  8. [Unit]
  9. Description=PostgreSQL database server
  10. Documentation=man:postgres(1)
  11. [Service]
  12. Type=notify
  13. User=pgsql
  14. ExecStart= /postgresql/pg12/bin/postgres -D /postgresql/pgdata
  15. ExecReload=/bin/kill -HUP $MAINPID
  16. KillMode=mixed
  17. KillSignal=SIGINT
  18. TimeoutSec=0
  19. [Install]
  20. WantedBy=multi-user.target
  21. ----
  22. chmod +x /etc/systemd/system/postgresql.service
  23. service postgresql start
  24. 数据库 一般不 开机启动

image_1ekj68iv11mbp1ufv19ukmeu1hft2n.png-170.5kB


  1. 连接数据
  2. psql -h127.0.0.1 -p5432

image_1ekj6u5hhmdsdgg1csg15lucbo34.png-58.7kB

  1. 设置密码:
  2. \password postgres (设置密码:123.com)
  3. 或者:
  4. alter user postgres with password '123.com';

image_1ekj72hkq1krn1r16n7f12d91f4t3h.png-40.6kB

  1. psql -h192.168.100.11 -U postgres -p5432

image_1ekj74sa31nc416qp16cs6hjnhd3u.png-68.3kB


  1. create user flyfish with password '123.com' nocreatedb;
  2. create database flyfishdb with owner=flyfish;

image_1ekj7jvmfo1811g8a2i1p0r1dou4b.png-65.5kB


  1. psql -h 192.168.100.11 -p 5432 -U flyfish -d flyfishdb
  2. create table flyfishtable(name varchar(50));
  3. insert into flyfishtable values('haha');
  4. commit ;
  5. select * from flyfishtable;

image_1ekj7mjbtsccam01vds1g7o2u4o.png-131.9kB

image_1ekj7mtlg1o63oh21umv9pun0m55.png-28.3kB


  1. \c flyfishdb flyfish 切换 数据库
  2. \dt + 查看 数据库下面的表
  3. \dt flyfishtable 查看 表的 信息
  4. \d flyfishtable 查看表结构

image_1ekj8i75nebt1p6jmn71fl31mrp5i.png-54.6kB

image_1ekj8ol398gdmmv3pg1ft06nd6c.png-40.9kB


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注