Postgresql
1、创建数据库、查看数据库
postgres-# CREATE DATABASE runoobdb; ERROR: syntax error at or near "CREATE" LINE 2: CREATE DATABASE runoobdb; ^ postgres=# CREATE DATABASE runoobdb; CREATE DATABASE postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+----------------------- postgres | postgres | UTF8 | C | C | runoobdb | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (4 rows)
2、查看表
postgres=# \d 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)
3、当存在多个数据库时,如何连接到指定的数据库
postgres-# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+----------------------- dayudb | postgres | UTF8 | C | C | postgres | postgres | UTF8 | C | C | runoobdb | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (5 rows) postgres-# \c dayudb You are now connected to database "dayudb" as user "postgres". dayudb-#
或者也可以通过ip加端口的方式连接到数据库,默认采用的如下方式连接:
oracle:~ dayu$ /Library/PostgreSQL/11/scripts/runpsql.sh
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]: postgres
Password for user postgres:
psql (11.4)
Type "help" for help
4、创建用户
dayudb=# create user dayu with password 'dayu'; CREATE ROLE
dayudb=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | company | table | postgres
public | test | table | postgres
(2 rows)
dayudb=# GRANT ALL PRIVILEGES ON database dayudb to dayu;
GRANT
dayudb=# \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | company | table | postgres
public | test | table | postgres
(2 rows)