PostgreSQL
安装
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql16-server
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
sudo systemctl enable postgresql-16
sudo systemctl start postgresql-16
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql12-server
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12
https : //yum.postgresql.org/12/redhat/rhel-7-x86_64/repoview/postgresqldbserver12.group.html
wget https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-server-12.20-2PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-contrib-12.20-2PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-12.20-2PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/12/redhat/rhel-7-x86_64/postgresql12-libs-12.20-2PGDG.rhel7.x86_64.rpm
wget https://get.enterprisedb.com/postgresql/postgresql-10.23-1-linux-x64-binaries.tar.gz
useradd postgres
password postgres
tar xvf postgresql-10.23-1-linux-x64-binaries.tar.gz -C /opt
mkdir -p /opt/pgsql/data
mkdir -p /opt/pgsql/log
chown -R postgres.postgres /opt/pgsql
su - postgres
vim ~/.bash_profile
export PG_HOME=/opt/pgsql
export PGDATA=${PG_HOME}/data
export PATH=${PG_HOME}/bin:$PATH
source ~/.bash_profile
${PG_HOME}/bin/initdb -E utf8
vim ${PGDATA}/postgresql.conf
listen_addresses = '*'
port = 5432
vi ${PGDATA}/pg_hba.conf
host all all 0.0.0.0/0 md5
${PG_HOME}/bin/pg_ctl start
${PG_HOME}/bin/pg_ctl stop
${PG_HOME}/bin/pg_ctl reload
psql -U postgres -d postgres
create user demo with password 'demo';
create database demo with encoding='utf8' owner=demo;
alter user demo password 'xxx';
psql -h <服务器IP或域名> -U <用户名> -d <数据库名> -p <端口号>
这里是具体的参数解释:
-h 用于指定服务器的IP地址或域名。
-U 用于指定数据库用户。
-d 用于指定要连接的数据库名。
-p 用于指定服务器上PostgreSQL监听的端口号
Copy
源码安装
user add postgres
passwd postgres
wget https://ftp.postgresql.org/pub/source/v12.20/postgresql-12.20.tar.gz
yum groupinstall -y "Development Tools" "Legacy UNIX Compatibility"
yum install -y bison flex readline* zlib-devel gcc* gmake
mkdir -p /usr/local/pg12
mkdir -p /pgdata/12/data
chown -R postgres. /pgdata
chown -R postgres. /usr/local/pg12
chmod 700 /pgdata/12/data -R
vim /etc/sysctl.conf
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
sysctl -p
numactl --show
dmesg | grep -i numa
cat /proc/cmdline
grubby --update-kernel=/boot/vmlinuz-$(uname -r) --args="numa=off"
grub2-mkconfig -o /etc/grub2.cfg
vim /etc/sysctl.conf
vm.zone_reclaim_mode = 0
kernel.numa_balancing = 0
numactl --interleave=all pg_ctl start -D $PGDATA
cat /sys/block/xvda/queue/scheduler
echo noop > /sys/block/xvda/queue/scheduler
vim /etc/default/grub
grubby --update-kernel=ALL --args="elevator=noop"
tar -xf postgresql-12.20.tar.gz
cd postgresql-12.20
./configure --prefix=/usr/local/pg12 --with-pgport=5432
gmake world
gmake install -world
su postgres
cd ~
vim .bash_profile
export PGDATA=/pgdata/12/data
export LANG=en_US.utf8
export PGHOME=/usr/local/pg12
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
source .bash_profile
psql --version
su - postgres
initdb -D /pgdata/12/data -w
initdb -A md5 -D $PGDATA -E utf8 --locale=C -W # 生产库建议
-A 是加密方式
-D 是数据目录
-E 是字符集 (默认即utf8)
pg_ctl -D /pgdata/12/data/ -l logfile start
pg_ctl -D /pgdata/12/data/ stop -ms
pg_ctl -D /pgdata/12/data/ stop -mf
pg_ctl -D /pgdata/12/data/ stop -mi
/opt/postgresql-12.12/contrib/start-scripts/linux
Copy
测试
psql
\l
create database test1 with owner=postgres encoding='UTF8' template=template0 lc_collate='C' lc_ctype='C' allow_connections=true;
drop database test1;
\c test1;
create table t1(id int);
insert into t1 values(1);
select * from t1;
cd $PGDATA
vim pg_hba.conf
host DBname username 10.10.10.0/24 md5
host all all 0.0.0.0/0 md5
vim postgresql.conf
listen_addresses = '0.0.0.0'
pg_ctl restart -mf
psql -d postgres -h 10.10.10.51 -p 5432 -U postgres
postgresql.conf配置文件,调整以下参数(优化连接数和性能的)
max_connections值建议设置成1000以上
shared_buffers值建议设置成操作系统总内存的1/4
work_mem值建议设置成8MB
cd /var/lib/pgsql/14/data
vim postgresql.conf
编辑postgresql.conf文件,追加以下参数(根据实际追加):
log_filename = 'postgres-%d.log'
log_rotation_age = 1440
log_rotation_size = 2097150
log_truncate_on_rotation = on
log_destination = 'stderr'
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
log_temp_files = 0
log_autovacuum_min_duration = 0
log_error_verbosity = default
log_statement = 'ddl'
log_duration = off
lc_messages = 'C'
log_min_duration_statement = 2000
max_connections = 1500
shared_buffers = 4096MB
work_mem = 8MB
listen_addresses = '*'
Copy
常用命令
\?
\h
\l
\c db1
\d
\dt
\d t1
\help create user
\du
select * from pg_tables where schemaname not in ('pg_catalog','information_schema');
\x
psql -d nextcloud -h 172.20.223.208 -p 5432 -U nextcloud lDLYPk35FvF7P48pSrjRw16O5W7NlXJ1wHWMqlXiB4m4miKV9c
pg_dump -U nextcloud -h localhost -p 5432 -F c -b -v -C -f /tmp/nextcloud.bakcup nextcloud
pg_restore -U postgres -h localhost -p 5432 -C -d postgres -v /var/lib/postgresql/data/bak//nextcloud.bakcup
GRANT ALL PRIVILEGES ON DATABASE nextcloud TO postgres;
GRANT ALL PRIVILEGES ON TABLE oc_migrations TO postgres;
GRANT ALL PRIVILEGES ON ALL DATABASES TO postgres;
REVOKE CONNECT ON DATABASE dbname FROM PUBLIC;
GRANT CONNECT ON DATABASE dbname TO PUBLIC;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO postgres;
Copy