使用Docker来运行PostgreSQL

图像下载

docker pull postgres:latest

确认

$ docker images
REPOSITORY             TAG           IMAGE ID       CREATED        SIZE

postgres               latest        bf700010ce28   2 days ago     379MB

启动

docker run -d --name=postgres -p 5432:5432 -e POSTGRES_PASSWORD=tiger123 postgres

使用bash连接

docker exec -it postgres bash
$ docker exec -it postgres bash
root@4ba8607d4e37:/# cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@4ba8607d4e37:/#

连接到数据库

psql -U postgres -h 127.0.0.1
$ psql -U postgres -h 127.0.0.1
Password for user postgres: 
psql (15.2 (Ubuntu 15.2-1))
Type "help" for help.

postgres=# 

创建用户和数据库

postgres=# create user scott with password 'tiger123';
CREATE ROLE
postgres=# create database city;
CREATE DATABASE
postgres=# alter database city owner to scott;
ALTER DATABASE
postgres=#

使用创建的用户连接到创建的数据库。

psql -U scott city -h 127.0.0.1
$ psql -U scott city -h 127.0.0.1
Password for user scott: 
psql (15.2 (Ubuntu 15.2-1))
Type "help" for help.

city=> 
广告
将在 10 秒后关闭
bannerAds