PostgreSQL使用记录

本文档提供了PostgreSQL的基础入门指导,包括安装解决方法、配置文件编辑、表空间与数据库创建步骤、序列管理、用户权限设定等核心内容,并介绍了常用的备份与恢复方法。
PostgreSQL新手入门[url]http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html[/url]

windows 2003安装问题:
1. problem running post install setup
安装路径权限问题,配置Users的权限为完全控制,就可以了.


两个配置文件:
sudo gedit /var/lib/pgsql/9.3/data/postgresql.conf
sudo gedit /var/lib/pgsql/9.3/data/pg_hba.conf

启动:[color=darkblue]sudo systemctl restart postgresql-9.3.service[/color]

pgAdmin 使用总结及postgreSQL常用操作 [url]http://blog.sina.com.cn/s/blog_6d2bb4da0100xh6n.html[/url]


[color=red][b]创建表空间[/b][/color]:你创建表空间的用户是postgres,那么,在修改此目录的控制权限的时候
[color=darkblue]sudo mkdir /var/tablespace
sudo chown -R postgres:postgres /var/tablespace
su postgres
psql test;
CREATE TABLESPACE tablespacename OWNER postgres LOCATION '/var/tablespace';[/color]


[color=red][b]创建数据库[/b][/color]
[color=darkblue]su postgres[/color] 登陆用户,如果不存在可以创建
[color=darkblue]createdb test[/color] 创建数据库
[color=darkblue]psql test[/color] 切换到test数据库
[color=darkblue]CREATE ROLE testuser WITH SUPERUSER LOGIN PASSWORD 'test';[/color] 创建角色
[color=blue]CREATE USER kanon PASSWORD 'kanon2';[/color] 创建账户 CREATE USER除了默认具有LOGIN权限之外,其他与CREATE ROLE是完全相同的。
[color=darkblue]psql -h localhost -U testuser test[/color] 测试账户
使用模板template0创建数据库两种方法
(1)[color=darkblue]createdb -T template0 dbname[/color]
(2)[color=darkblue]create database dbname template=template0[/color]
[color=darkblue]CREATE DATABASE aaaa WITH ENCODING='UTF8' OWNER=testuser TEMPLATE=template0 CONNECTION LIMIT=10 TABLESPACE=tablespacename;[/color]
[color=red][b]创建数据库是指定表空间[/b][/color]:
[color=darkblue]CREATE DATABASE dbname OWNER kanon TEMPLATE template1 TABLESPACE tablespacename;[/color]


[color=red][b]备份恢复工具[/b][/color]
[color=red]Barman[/color] 1.4.0 发布,PostgreSQL 备份和恢复管理器 [url]http://www.oschina.net/news/59146/barman-1-4-0[/url]
备份和恢复PostgreSQL数据库 [url]http://publish.it168.com/2005/0825/20050825099001.shtml[/url]
PostGresql 备份和恢复 [url]http://zwb8686.blog.51cto.com/982393/1140172[/url]
[color=red][b]PgSql备份pg_dump与还原手记pg_restore[/b][/color][url]http://www.cnblogs.com/wangbin/archive/2009/08/14/1546009.html[/url]
[color=darkblue]
//////备份:
pg_dump -h localhost -p 5432 -U testuser test>pandy_test.bak
pg_dump -h 192.168.0.198 -p 5432 -b -c -C -Fc -U postgres rhcpm_dev4>/home/pandy/rhcpm_dev4.backup //-Fc之间有没有空格, 好像都一样.
/////恢复:
I:
pg_restore -h 192.168.2.102 -p 5432 -U postgres -W -d rhcpm_dev4 -v /home/pandy/桌面/rhcpm_dev4-2015-06-19_172242.backup
II:
\set ON_ERROR_STOP
psql -h localhost -p 5432 -U testuser test<pandy_test.bak
[/color]


[color=red][b]数据从服务器复制到本地数据库[/b][/color]
[b]自己些的代码 [/b]
[color=darkblue]export date_str=$(date "+%Y-%m-%d_%H%M%S")
echo $date_str
pg_dump -h 192.168.0.167 -p 5432 -b -c -C -Fc -U postgres rhcpm_dev4>/home/pandy/桌面/rhcpm_dev4-"$date_str".backup;
pg_restore -h 192.168.2.102 -p 5432 --if-exists -c -U postgres -W -d rhcpm_dev4 -v /home/pandy/桌面/rhcpm_dev4-"$date_str".backup;[/color]

[b]pgadminIII工具看到的代码:[/b]
[color=darkblue]export date_str=$(date "+%Y-%m-%d_%H%M%S")
echo $date_str
pg_dump --host 192.168.0.5 --port 5432 --username "postgres" --role "postgres" --no-password --format custom --blobs --verbose --file "/home/pandy/桌面/rhcpm_saas-$date_str.backup" "rhcpm_saas"
pg_restore --host 192.168.0.5 --port 5432 --username "postgres" --dbname "rhcpm_saas_test" --role "postgres" --no-password --verbose "/home/pandy/桌面/rhcpm_saas-$date_str.backup"[/color]


[color=red][b]自动备份[/b][/color]
Windows [url]http://www.oschina.net/question/100267_71299[/url]
PostgreSQL自动备份的批处理文件 [url]http://blog.youkuaiyun.com/adeng1919/article/details/13022859[/url]
@ECHO OFF
echo 正在初始化环境变量. . .
echo.
:: 对于路径中有空格的路径字符串,需要加上英文双引号包裹!否则就会出现命令错误!
set POSTGRESQL_DIR="C:\PostgreSQL\9.3\bin"
set DB_NAME=rhcpm_dev4
set USER="postgres"
:: 数据库管理系统名
set DBMS_NAME=PostgreSQL
set SV_IP="192.168.0.167"
set SV_PORT="5432"
set DBBAK_DIR="C:\PostgreSQL\pg_db_bak"
set CMD_STR="-b -c -C -Ft -U"
:: PostgreSQL的pg_dump命令行工具没有带密码的参数,但是可以设置一个PGPASSWORD的环境变量来提供密码
set PGPASSWORD=1
echo 正在备份数据库. . .
echo.
:: 以下是获得当前系统时间的命令,e.g. 20120503101305
:: 年
set myyy=%date:~0,4%
:: 月
set mymm=%date:~5,2%
:: 日
set mydd=%date:~8,2%
::echo %myyy% %mymm% %mydd%
set /a TODAY=%date:~0,4%%date:~5,2%%date:~8,2%
set _TIME=%time:~0,8%
::echo %_TIME%
set CURRENTTIME=%_time::=%
set CURRENTTIME=%CURRENTTIME: =0%
::set MYDATETIME=%TODAY%-%CURRENTTIME%
::set MYDATETIME=%TODAY%-%_TIME%
::set MYDATETIME=%myyy%-%mymm%-%mydd%_%CURRENTTIME%
set MYDATETIME=%TODAY%_%CURRENTTIME%
D:
cd %POSTGRESQL_DIR%
:: 使用PostgreSQL提供的pg_dump命令将具体数据库导出为.sql文件
%POSTGRESQL_DIR%\pg_dump.exe -h %SV_IP% -p %SV_PORT% -b -c -C -Ft -U %USER% %DB_NAME%>%DBBAK_DIR%\%DB_NAME%_%MYDATETIME%.backup
echo 备份结束
echo
exit

PostgreSQL自动备份(backup)与恢复(restore)数据库(图解)[url]https://xly3000.wordpress.com/2012/03/07/postgresql%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BDbackup%E4%B8%8E%E6%81%A2%E5%A4%8Drestore%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9B%BE%E8%A7%A3/[/url]
Linux [url]http://mlm.iteye.com/blog/1129709[/url]


[color=red][b]序列[/b][/color]
postgresql 创建序列方法_postgresql create sequence [url]http://www.jsjtt.com/shujuku/postgresql/32.html[/url]
PostgreSQL 序列(SEQUENCE) [url]http://www.cnblogs.com/mchina/archive/2013/04/10/3012493.html[/url]
PostgreSQL 8.1 序列操作函数 [url]http://www.php100.com/manual/PostgreSQL8/functions-sequence.html[/url]
[color=darkblue]-- DROP SEQUENCE seq_user_id;
CREATE SEQUENCE seq_user_id INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1;
ALTER TABLE seq_route_id OWNER TO postgres;[/color]

[color=darkblue][b]查找所有数据库[/b][/color]
SELECT datname FROM pg_database;

[color=red][b]查找所有用户表[/b][/color]
SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' ORDER BY tablename;


[color=red][b]查找表的所有字段[/b][/color]
SELECT a.attnum,a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull from pg_class c,pg_attribute a,pg_type t where c.relname='rh_dimension' and a.attnum>0 and a.attrelid=c.oid and a.atttypid=t.oid



[color=red][b]postgres查询表中所有字段、主键、唯一、外键、是否为空[/b][/color]
select 'true' as list,'true' as edit,'false' as search,a.attname as column_name,format_type(a.atttypid,a.atttypmod) as data_type,
(case
when atttypmod-4>0 then atttypmod-4
else 0
end)data_length,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0 then 'Y'
else 'N'
end) as P,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then 'Y'
else 'N'
end) as U,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then 'Y'
else 'N'
end) as R,
(case
when a.attnotnull=true then 'Y'
else 'N'
end) as nullable,
col_description(a.attrelid,a.attnum) as comment,'XEditText' as control
from pg_attribute a
where attstattarget=-1 and attrelid = (select oid from pg_class where relname ='userinfo')


[color=red][b]日期格式化[/b][/color]
SELECT to_char(CURRENT_DATE,'yyyy-MM-dd hh24:MI:ss')

[color=red][b]查找时间[/b][/color]
SELECT current_date,current_time

[color=red][b]当前时间[/b][/color]
SELECT now()

[color=red][b]插入时间[/b][/color]
INSERT INTO _sequence_table(create_date) VALUES (DATE '2001-02-16');

[color=red][b]时间日期[/b][/color]
SELECT TIMESTAMP '2001-02-16 20:38:40';

[color=red][b]创建日期时间类型和保存方式[/b][/color]
create_datetime timestamp without time zone,
to_timestamp('2013-05-20 18:29:42','yyyy-mm-dd hh24:mi:ss')

[color=red][b]替换[/b][/color]
replace(tablename, 'tb_', '')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值