postgresql安装
postgresql安装
前提条件
make
3.80版本以上
make --version
GNU Make 3.82
readline
zlib
2.安装部署
创建用户
groupadd postgres
useradd -g postgres postgres
passwd postgres
创建目录
df -Th
mkdir -p /data/postgres/13.3
chown -R postgres:postgres /data
安装
以postgres账号登录
tar zxvf postgresql-13.3.tar.gz
cd postgresql-13.3
./configure --help
./configure --prefix=/data/postgres/13.3
初始化数据库
/data/postgres/13.3/bin/initdb -D /data/postgres/13.3/data
[postgres@db01 data]$ ll
total 52
drwx------ 5 postgres postgres 41 May 22 17:51 base
drwx------ 2 postgres postgres 4096 May 22 17:51 global
drwx------ 2 postgres postgres 6 May 22 17:50 pg_commit_ts
drwx------ 2 postgres postgres 6 May 22 17:50 pg_dynshmem
-rw------- 1 postgres postgres 4760 May 22 17:50 pg_hba.conf
-rw------- 1 postgres postgres 1636 May 22 17:50 pg_ident.conf
drwx------ 4 postgres postgres 68 May 22 17:51 pg_logical
drwx------ 4 postgres postgres 36 May 22 17:50 pg_multixact
drwx------ 2 postgres postgres 6 May 22 17:50 pg_notify
drwx------ 2 postgres postgres 6 May 22 17:50 pg_replslot
drwx------ 2 postgres postgres 6 May 22 17:50 pg_serial
drwx------ 2 postgres postgres 6 May 22 17:50 pg_snapshots
drwx------ 2 postgres postgres 6 May 22 17:50 pg_stat
drwx------ 2 postgres postgres 6 May 22 17:50 pg_stat_tmp
drwx------ 2 postgres postgres 18 May 22 17:50 pg_subtrans
drwx------ 2 postgres postgres 6 May 22 17:50 pg_tblspc
drwx------ 2 postgres postgres 6 May 22 17:50 pg_twophase
-rw------- 1 postgres postgres 3 May 22 17:50 PG_VERSION
drwx------ 3 postgres postgres 60 May 22 17:50 pg_wal
drwx------ 2 postgres postgres 18 May 22 17:50 pg_xact
-rw------- 1 postgres postgres 88 May 22 17:50 postgresql.auto.conf
-rw------- 1 postgres postgres 28023 May 22 17:50 postgresql.conf
设置环境变量
vi .bash_profile
export PGDATA=/data/postgres/13.3/data
export PATH=/data/postgres/13.3/bin:$PATH
psql
\q
pg_hba.conf
postgres.conf
listen_addresses = '*'
重启
关闭
启动
如果设置了PGDATA环境变量:
pg_ctl start -l logfile
查看状态
pg_ctl status
pg_ctl: server is running (PID: 2647)
/data/postgres/13.3/bin/postgres
pstree -p 2647
postgres(2647)─┬─postgres(2649)
├─postgres(2650)
├─postgres(2651)
├─postgres(2652)
├─postgres(2653)
└─postgres(2654)
3.psql使用
psql连接数据库
\c 列出当前用户
\d 列出对象
create table t1(name text);
\d t1 列出表结构
\h create table 查看帮助
select version();
\l 列出数据库
\l+ 包含表空间、大小
select pg_postmaster_start_time();数据库启动时间
\du 查看用户
\dt+ t1 查看t1表大小
\di 查看索引
create index idx_name on t1(name);
\di+ idx_name 查看索引大小
创建用户:
\h create user
\h create role
create user fx superuser password '123456';
创建数据库:
create database testdb owner fx;
\c testdb fx 使用fx用户连接testdb
查看表空间:
\db
查看视图:
\dv