一、安装
使用 docker 安装:
$ docker pull postgres:alpine
二、启动
起 docker 容器:
$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres:alpine
默认用户:postgres
默认密码:mysecretpassword
或者通过
psql
,验证不成功:$ docker run -it --rm --network some-network postgres:alpine psql -h some-postgres -U postgres:alpine
报错:
docker: Error response from daemon: network some-network not found.
使用容器连接:
$ docker run --rm -t -i --link some-postgres:pg postgres:alpine bash
在客户端 docker run
中直接使用 -link remote_name:local_alias
使容器连接到另一个容器端口。比如上面的例子就是使容器 some-postgres 连接到另一个容器 pg。
然后,通过 psql
:
$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d postgres -U postgres --password
注意用户要是之前的默认用户 postgres
然后会出现输入密码,输入之前设置的默认密码:mysecretpassword
以下界面表示成功连接到 PostgreSQL:
psql (13.3)
Type "help" for help.
postgres=#
三、PSQL 操作数据库
psql命令
Object Type | All Privileges | Default PUBLIC Privileges |
psql Command |
---|---|---|---|
DATABASE |
CTc |
Tc |
\l |
DOMAIN |
U |
U |
\dD+ |
FUNCTION or PROCEDURE |
X |
X |
\df+ |
FOREIGN DATA WRAPPER |
U |
none | \dew+ |
FOREIGN SERVER |
U |
none | \des+ |
LANGUAGE |
U |
U |
\dL+ |
LARGE OBJECT |
rw |
none | |
SCHEMA |
UC |
none | \dn+ |
SEQUENCE |
rwU |
none | \dp |
TABLE (and table-like objects) |
arwdDxt |
none | \dp |
Table column | arwx |
none | \dp |
TABLESPACE |
C |
none | \db+ |
TYPE |
U |
U |
\dT+ |
中文补充
操作 | 命令 |
---|---|
查看所有数据库 | # \l |
切换数据库 | # \c dbname |
查看当前数据库中的所有表 | # \d |