数据库基础之PostgreSQL

安装

server端

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y

yum install postgresql11-server -y

client端

yum install postgresql11 -y

初始化

/usr/pgsql-11/bin/postgresql-11-setup initd

修改配置

vim /var/lib/pgsql/11/data/postgresql.conf
listen_address = '*'

vim /var/lib/pgsql/11/data/pg_hba.conf
host all all 0.0.0.0/0 md5

启动

systemctl start postgresql-11

登录

sudo -su postgres psql -U postgres

创建账号和权限

CREATE ROLE test WITH LOGIN;
ALTER ROLE test WITH PASSWORD 'test';

GRANT ALL ON DATABASE mydb TO test;
GRANT ALL ON SCHEMA myschema TO test;
GRANT ALL ON TABLE mytable TO test;

连接

psql -h $host -p $port -U $user $db

查看库和表

show databases:\l
use $database:\c $database
show tables:\dt
desc table:\d $table
change schema:set search_path to $schema

查看表结构

SELECT a.attname as name, format_type(a.atttypid,a.atttypmod) as type, col_description(a.attrelid,a.attnum) as comment 
FROM pg_attribute as a, pg_namespace b, pg_class as c
where b.nspname = '${schema}' and b.oid = c.relnamespace and c.relname = '${table}' and a.attrelid = c.oid and a.attnum > 0 and a.attname not like '.%';
posted @ 2021-09-17 22:08  匠人先生  阅读(147)  评论(0编辑  收藏  举报