Postgresql 教程

Official 教程

关闭postgresql服务

PostgreSQL帐号

1. PostgreSQL 用户帐号和操作系统用户帐号是不同的,系统用户帐号是postgres.

sudo -u postgres -i 

Use the operating system user postgres to create your database - as long as you haven't set up a database role with the necessary privileges that corresponds to your operating system user. 

2. create new users.

sudo -u postgres createuser owning_user

here

创建数据库

1. 以postgres - operating system user 登录: sudo -u postgres -i

2. 创建数据库: createdb mydb

3. 删除数据库: dropdb mydb

访问数据库: psql控制台

from here.

一旦你创建了数据库,你就可以通过以下方式访问它: 

  • 运行PostgreSQL的交互式终端程序,它被称为psql, 它允许你交互地输入、编辑和执行SQL命令。psql mydb

  • 使用一种已有的图形化前端工具,比如pgAdmin或者带ODBCJDBC支持的办公套件来创建和管理数据库。这种方法在这份教程中没有介绍。

  • 使用多种绑定发行的语言中的一种写一个自定义的应用。这些可能性在第 IV 部分中将有更深入的讨论。

退出psql控制台: q

退出user shell: exit

SQL语言

每个表都是一个命名的集合。一个给定表的每一行由同一组的命名组成,而且每一列都有一个特定的数据类型。虽然列在每行里的顺序是固定的, 但一定要记住 SQL 并不对行在表中的顺序做任何保证(但你可以为了显示的目的对它们进行显式地排序)

PostgreSQL 语法


PostgreSQL连接数据库的两种方式

from here

1. pgAdmin和psql,pgAdmin是可视化工具,psql是命令行工具。

虽然pgAdmin操作起来会更加直观简单,但是在restore和backup db的时候,效率和性能会比较低下,如果db过于庞大,还会导致pgAdmin内存溢出。推荐使用psql来连接数据库进行备份和恢复db,同样大小的db,使用psql来restore会比pgAdmin快上数倍!

psql -h <dbserver_IP> -p<dbserver_port> -d <database_Name> -U <db user>
psql -h <dbserver_IP> -p<dbserver_port> -U <db user>    #不指定数据库连接
psql -d test -U postgres   #connect localhost database

postgres=# c mydb    #c <database_Name>     #连接某个数据库

2. Using docker to install pgAdmin4 

Remember to change your email address, passwd, and so on. If you forget to do that, you can still login on the system by:

  • Email: youremail@yourdomain.com
  • Password: yoursecurepassword

Docker has three default networking options:

  • bridge - the default network for a container, it represents a virtual, isolated network for you containers
  • host - the network on your computer; any container on this network is potentially visible to the Internet
  • none - no network is assigned to a container
原文地址:https://www.cnblogs.com/dulun/p/12198934.html