docker pull orchardup/postgresql
docker run -d -p 5432:5432 -e POSTGRESQL_USER=test -e POSTGRESQL_PASS=oe9jaacZLbR9pN -e POSTGRESQL_DB=test orchardup/postgresql
docker exec -it a397deee6810 /bin/bash
## 使用test用户连接到test数据库
psql -h localhost -U test test
## 查看有什么数据库
test=# \l
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
test | test | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
## 切换到test数据库
test=# \c test
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
You are now connected to database "test" as user "test".
test=#
## 查看表
test=# \dt
No relations found.
## 创建表
test=# create table test2 (id integer,name text);
CREATE TABLE
## 查看表
test=# \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+-------
public | test2 | table | test
(1 row)
## 插入数据
test=# insert into test2 values(1,'david');
INSERT 0 1
## 查询表中数据
test=# select * from test2;
id | name
----+-------
1 | david
(1 row)
## 更新表中记录
test=# update test2 set fullname = 'ryan' where id=1;
UPDATE 1
test=#
## 查看表结构
test=# \d test2
id | integer |
name | text |
## 修改表结构,增加一个字段
test=# alter table test2 add column age integer;
ALTER TABLE
test=# \d test2
id | integer |
name | text |
age | integer |
## 修改表结构,删除一个字段
test=# alter table test2 drop column age;
ALTER TABLE
test=# \d test2
id | integer |
name | text |
## 修改表结构,重命名一个字段
test=# alter table test2 rename column name to fullName ;
ALTER TABLE
test=# \d test2
id | integer |
fullname | text |
## 首先,创建数据库用户dbuser,并指定其为超级用户。
root@a397deee6810:/# sudo -u postgres createuser --superuser dbuser
## 登录数据库控制台,设置dbuser用户的密码,完成后退出控制台。
root@a397deee6810:/# sudo -u postgres psql
psql (9.3.5)
Type "help" for help.
postgres=# \password dbuser
Enter new password:
Enter it again:
postgres=# \q
could not save history to file "/var/lib/postgresql/.psql_history": No such file or directory
## 在shell命令行下,创建数据库exampledb,并指定所有者为dbuser。
root@a397deee6810:/# sudo -u postgres createdb -O dbuser exampledb
root@a397deee6810:/# sudo -u postgres psql
psql (9.3.5)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
exampledb | dbuser | 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
test | test | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)
## 使用dbuser用户登录,切换到数据库exampledb
root@a397deee6810:/# psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432
## 导入sql文件数据到test表中
root@a397deee6810:/# vi test.sql
insert into test2 values(2,'aaa');
insert into test2 values(3,'bbb');
insert into test2 values(4,'ccc');
root@a397deee6810:/#
root@a397deee6810:/# psql -U test -d test < test.sql
INSERT 0 1
INSERT 0 1
INSERT 0 1
root@a397deee6810:/# psql -U test -d test -h 127.0.0.1 -p 5432
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
test=# select * from test2;
id | fullname
----+----------
1 | ryan
2 | aaa
3 | bbb
4 | ccc
(4 rows)
test=#

最新文章

  1. js-JavaScript高级程序设计学习笔记12
  2. json对象,数组,字符串总结
  3. 分布式Hadoop安装(一)
  4. 解决setInterval计时器不准的问题
  5. CAGradientLayer实现色差动画
  6. openwrt 汉化
  7. 亲身体验:digitalocean vps能做的10件事
  8. hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组/线段树)
  9. [UE4]接口
  10. 阿里云.log
  11. struts2框架之请求参数(参考第二天学习笔记)
  12. prometheus-dashboard-to-grafana
  13. spring cloud单点登录
  14. 【Java】线程池的作用
  15. 一台服务器的IIS绑定多个域名
  16. RTX Server SDK跨服务器如何调用
  17. 【UI】自动化用例设计技巧
  18. 对”唯一键可以包含NULL值,并且每个NULL值都是唯一的(即NULL!=NULL)“理解
  19. Java_数据交换_Jackson_用法入门
  20. CFG的定义

热门文章

  1. openlayers应用(二):加载百度离线瓦片
  2. 多源最短路Floyd 算法————matlab实现
  3. 关于“foreach循环”中遇到的几个问题总结
  4. 分针网—IT教育: Html / CSS常见问题的解决方案
  5. SVN常见问题总结一
  6. iOS之RunLoop
  7. Struts2之 OGNL表达式和值栈
  8. 如何为mysql添加、启动服务
  9. SQL Sever2008 新手入门第一天安装软件
  10. 随笔-关于公网IP无法访问服务器的解决办法