官方下载地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
https://www.postgresql.org/ftp/source/
postgres中文文档:http://www.postgres.cn/docs/9.3/
常用命令
1、创建数据库
create database mydb;
2、删除
drop database mydb;
3、切换数据库:\c [database]
\c mydb;
4、切换用户:\c [username]
\c -postgres;
5、创建表
create table demo(
id int,
name varchar(20),
sex int,
age int,
date date
);
6、查看表结构
\d demo;
7、修改PostgreSQL数据库默认用户postgres的密码
ALTER USER postgres WITH PASSWORD 'postgres';
8、查询表对应的文件名
mydb=# select oid from pg_class where relname='demo';
oid
-------
16402
(1 行记录)
9、查询数据库名及对应的文件
select datname,oid from pg_database
10、添加用户
psql> create user apache CREATEDB CREATEUSER;
psql> create user oracle CREATEDB CREATEUSER;
11、批量删除表
CREATE FUNCTION del_ora_table() RETURNS void AS $$
DECLARE
tmp VARCHAR(512);
DECLARE names CURSOR FOR
select tablename from pg_tables where schemaname='public';
BEGIN
FOR stmt IN names LOOP
tmp := 'DROP TABLE '|| quote_ident(stmt.tablename) || ' CASCADE;';
RAISE NOTICE 'notice: %', tmp;
EXECUTE 'DROP TABLE '|| quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
RAISE NOTICE 'finished .....';
END;
$$ LANGUAGE plpgsql;
--执行函数批量删除表
select del_ora_table();
12、计划任务备份
crontab -e
* * * * * /dbback.sh
#!/bin/sh
su postgres -c 'BKDATE="$(date +"%Y-%m-%d")"; /usr/local/pgsql/bin/pg_dump tpp | gzip > /SQLdata/dump/backup/tpp.$BKDATE.dump.gz'
window数据库备份
pg_dump.exe -U postgres -f /database.dump -t test01 --column-inserts database_name(数据库名)
13、postgres 数据恢复
gzip -d database_file.2021-05-30.dump
cat database_file.2021-05-31.dump | ./psql database_name
window 恢复数据库方法
psql.exe -U postgres -f D:\jf\backup\database.dump database_name
14、查看当前sql语句查询状态
select * from pg_stat_activity;