用于查看已经存在的数据库:
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileg
es
-----------+----------+----------+-------------+-------------+------------------
-----
mydb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
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/post
gres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)
使用 \c + 数据库名 来进入数据库:
postgres-# \c test
You are now connected to database "test" as user "postgres".
退出数据库的命令:
test-# \q
在系统的命令行查看,我么可以在连接数据库后面添加数据库名来选择数据库:
[postgres@localhost bin]$ ./psql -h localhost -p 5432 -U postgres mydb
命令解析./psql连接数据库的命令 -h localhost链接本地 -p 5432 连接数据库的地址 -U 登录数据库使用的用户 链接库内的表
删除数据库
postgres=# drop database test;
DROP DATABASE
系统命令窗口下删除数据库
进入到 PostgreSQL 的安装目录,并进入到 bin 目录,dropdb 名位于 PostgreSQL安装目录/bin 下,执行删除数据库的命令:
[postgres@localhost bin]$ ls
clusterdb initdb pg_ctl pg_resetwal pg_waldump
createdb pg_archivecleanup pg_dump pg_restore postgres
createuser pg_basebackup pg_dumpall pg_rewind postmaster
dropdb pgbench pg_isready pg_test_fsync psql
dropuser pg_config pg_receivewal pg_test_timing reindexdb
ecpg pg_controldata pg_recvlogical pg_upgrade vacuumdb
[postgres@localhost bin]$ ./dropdb -h localhost -p 5432 -U postgres mydb
命令解析: ./dropdb 启动删除数据库的命令
创建表格
postgres=# create database zhu; ----创建数据库zhu
postgres=# \c zhu ----进入数据库zhu
zhu=# create table company( -----创建一张company表格
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CREATE TABLE
zhu=# create table department( -----创建一张department表格
zhu(# ID INT PRIMARY KEY NOT NULL,
zhu(# DEPT CHAR(50) NOT NULL,
zhu(# EMP_ID INT NOT NULL
zhu(# );
CREATE TABLE
zhu=# \d ----\d 命令来查看表格是否创建成功
List of relations
Schema | Name | Type | Owner
--------+------------+-------+----------
public | company | table | postgres
public | department | table | postgres
(2 rows)
zhu=# \d company ---查看名为company 表格的信息
Table "public.company"
Column | Type | Collation | Nullable | Default
---------+---------------+-----------+----------+---------
id | integer | | not null |
name | text | | not null |
age | integer | | not null |
address | character(50) | | |
salary | real | | |
Indexes:
"company_pkey" PRIMARY KEY, btree (id)
zhu=# drop table department; ----删除表格信息
DROP TABLE
zhu=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | company | table | postgres
(1 row)
向表格中插入数据:
zhu=# insert into company(id,name,age,address,salary) values (1,'zhu',3,'sz',4000);
INSERT 0 1
zhu=# insert into company(id,name,age,address) values (2,'hai',3,'sz');
INSERT 0 1
zhu=# insert into company(id,name,age,address,salary) values (3,'yan',3,'lg',5000);
INSERT 0 1
zhu=# insert into company(id,name,age,address,salary) values (4,'cheng',5,'sh',6000);
INSERT 0 1
zhu=# insert into company(id,name,age,address,salary) values (5,'xi',6,'vj',7000);
表格查看数据:
1.查看表格中的所有数据:
zhu=# select * from company;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
1 | zhu | 3 | sz | 4000
2 | hai | 3 | sz |
3 | yan | 3 | lg | 5000
4 | cheng | 5 | sh | 6000
5 | xi | 6 | vj | 7000
(5 rows)
2.查看表格中的部分数据
zhu=# select ID,NAME FROM company;
id | name
----+-------
1 | zhu
2 | hai
3 | yan
4 | cheng
5 | xi
(5 rows)
3.按条件去查找数据
zhu=# select * from company where id=1;
id | name | age | address | salary
----+------+-----+----------------------------------------------------+--------
1 | zhu | 3 | sz | 4000
(1 row)
zhu=# select * from company where age>3 and salary>3000;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
4 | cheng | 5 | sh | 6000
5 | xi | 6 | vj | 7000
(2 rows)
zhu=# select * from company where age>3 or salary=4000;
id | name | age | address | salary
----+-------+-----+----------------------------------------------------+--------
1 | zhu | 3 | sz | 4000
4 | cheng | 5 | sh | 6000
5 | xi | 6 | vj | 7000
(3 rows)
更新表格的部分数据
zhu=# update company set salary=7000 where ID=3;
UPDATE 1
zhu=# update company set address='sz',salary=10000;
UPDATE 5
按调节删除数据
zhu=# delete from company company where id=2;