设为首页 加入收藏

TOP

Debian平台下Postgresql数据库基本操作说明(二)
2015-07-24 10:16:14 来源: 作者: 【 】 浏览:2
Tags:Debian 台下 Postgresql 数据库 基本 操作说明
ostgres=# \l

List of databases

Name | Owner | Encoding | Collate | Ctype | Access privileges

-----------+----------+----------+-------------+-------------+-----------------------

cndba | postgres | UTF8 |en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +

| | | | | postgres=CTc/postgres+

| | | | | dave=CTc/postgres

postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |

template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |=c/postgres +

| | | | | postgres=CTc/postgres

template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |=c/postgres +

| | | | | postgres=CTc/postgres

(4 rows)

?

?

--重置postgres账户密码:

postgres=# alter user postgres withpassword 'dave';

ALTER ROLE

?

--修改用户认证配置文件pg_hba.conf:

postgres@dave:/etc/postgresql/9.1/main$ pwd

/etc/postgresql/9.1/main

postgres@dave:/etc/postgresql/9.1/main$ ls

environment pg_ctl.conf pg_hba.conf pg_ident.conf postgresql.conf start.conf

?

# Database administrative login by Unixdomain socket

local all postgres trust

?

# TYPE DATABASE USER ADDRESS METHOD

?

# "local" is for Unix domainsocket connections only

local all all md5

?

--重新加载postgresql:

postgres@dave:/etc/postgresql/9.1/main$/etc/init.d/postgresql reload

?

postgres@dave:~$ psql -d cndba -U dave

Password for user dave:

psql (9.1.15)

Type "help" for help.

?

cndba=> selectcurrent_user;

current_user

--------------

dave

(1 row)

?

?

--表的基本操作:

?

postgres-> \c cndba

You are now connected to database"cndba" as user "dave".

cndba->

?

cndba=> create table cndba(namevarchar(20),signupdate date);

CREATE TABLE

cndba=> insert into cndba(name, signupdate)values('dave', '2015-02-11');

INSERT 0 1

cndba=> select * from cndba;

name| signupdate

------+------------

dave| 2015-02-11

(1 row)

?

cndba=> update cndba set name ='tianlesoftware' where name = 'dave';

UPDATE 1

cndba=> alter table cndba add emailvarchar(40);

ALTER TABLE

cndba=> alter table cndba alter columnsignupdate set not null;

ALTER TABLE

cndba=> alter table cndba rename columnsignupdate to signup;

ALTER TABLE

cndba=> alter table cndba drop columnemail;

ALTER TABLE

cndba=> alter table cndba rename todave;

ALTER TABLE

cndba=> drop table if exists dave;

DROP TABLE

cndba=>

?

?

4 postgresql 查看数据库,表,索引,表空间以及大小

?

postgres=# select pg_database.datname,pg_database_size(pg_database.datname) AS size from pg_database;

datname | size

-----------+---------

template1 | 6030136

template0 | 6030136

postgres | 6030136

cndba | 6038328

(4 rows)

?

--以KB,MB,GB的方式来查看数据库大小

postgres=# selectpg_size_pretty(pg_database_size('cndba'));

pg_size_pretty

----------------

5897kB

(1 row)

?

postgres=# create table cndba(namevarchar(20),signupdate date);

CREATE TABLE

?

--查看多表:

postgres=# \dt

List of relations

Schema | Name | Type | Owner

--------+-------+-------+----------

public | cndba | table | postgres

(1 row)

?

--查看单表:

postgres=# \d cndba

Table "public.cndba"

Column | Type | Modifiers

------------+-----------------------+-----------

name | character varying(20) |

signupdate | date |

?

?

--查看表大小

postgres=# select pg_relation_size('cndba');

pg_relation_size

------------------

8192

(1 row)

?

--以KB,MB,GB的方式来查看表大小

postgres=# selectpg_size_pretty(pg_relation_size('cndba'));

pg_size_pretty

----------------

8192bytes

(1 row)

?

?

--查看索引信息:

postgres=# create index idx_cndba oncndba(name);

CREATE INDEX

?

postgres=# \di

List of r

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SQLServer中DML语句要申请的锁 下一篇T_SQLrowtype类型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)