PostgreSQL - 创建数据库 创建数据表
程序员文章站
2024-03-21 12:49:04
...
文章目录
第一次登陆PG
像oracle一样, 虽然我们安装数据库的时候使用的是root超级用户, 但是一切的数据库操作都是受用 postgres 这个系统普通用户:
su - postgres
查看数据库版本
[[email protected] ~]$ psql --version
psql (PostgreSQL) 12.0
查看所有PG库
这点很像myql的结构, 一共有三个数据库
- postgres 系统库
- 剩下的两个是模板库
[[email protected] ~]$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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
(3 rows)
创建数据库
直接在操作系统下进行创建, 并查看
[[email protected] ~]$ createdb dd
[[email protected] ~]$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
dd | 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/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
使用数据库
[[email protected] ~]$ psql dd
psql (12.0)
Type "help" for help.
dd=# select now();
now
-------------------------------
2020-03-01 16:52:28.816911+08
(1 row)
dd=#
dd=#
dd=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), 64-bit
(1 row)
dd=#
dd=# exit
删除数据库
[[email protected] ~]$ dropdb dd
[[email protected] ~]$
[[email protected] ~]$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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
(3 rows)
Table 数据表
库让咱们删了, 现在创建学习的库 dong
:
[[email protected] ~]$ createdb dong
[[email protected] ~]$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
dong | 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/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
创建Table
- 先登录指定的数据库中
[[email protected] ~]$ psql dong
psql (12.0)
Type "help" for help.
dong=#
- 创建表语句, 基本上跟Oracle , mysql一样
dong=# create table t1 ( id int , name varchar(20) , content text);
CREATE TABLE
dong=#
查看当前库中所有表
dong=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t1 | table | postgres
(1 row)
查看表结构
dong=# \d t1
Table “public.t1”
Column | Type | Collation | Nullable | Default
---------±----------------------±----------±---------±--------
id | integer | | |
name | character varying(20) | | |
content | text | | |
修改表名
语句是不是很眼熟
dong=# alter table t1 rename to tab1;
ALTER TABLE
dong=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | tab1 | table | postgres
(1 row)
删除表
dong=# drop table tab1;
DROP TABLE
dong=# \dt
Did not find any relations.
引用操作系统文件脚本 \i
通过引用操作系统上的.sql
文件, 创建对象的方法
[[email protected] ~]$ vi ct.sql
create table t2 (id int , name varchar(10));
~ ## 保存并退出
使用 \i
import 导入文件中的内容:
[[email protected] ~]$ psql dong
psql (12.0)
Type "help" for help.
dong=# \dt
Did not find any relations.
dong=# \i ct.sql
CREATE TABLE
dong=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | t2 | table | postgres
(1 row)