安装postgres
scutech@infokist:~$ docker pull postgres Using default tag: latest ...... scutech@infokist:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE postgres latest b97bae343e06 4 weeks ago 313MB scutech@infokist:~$ docker run -itd -p 5432:5432 --name psql -e POSTGRES_PASSWORD=dingjia -d postgres 9fefa241f818f487d8084757cf5cd3912ec6730f7d0dc0bc9f3253ac314f481c scutech@infokist:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9fefa241f818 postgres "docker-entrypoint.s…" 8 minutes ago Up 8 minutes 0.0.0.0:5432->5432/tcp psql scutech@infokist:~$
进入postgres
scutech@infokist:~$ docker exec -it scutech bash root@408b5cf5586f:/# root@408b5cf5586f:/# su postgres postgres@408b5cf5586f:/$ psql psql (12.3 (Debian 12.3-1.pgdg100+1)) Type "help" for help. postgres=#
创建用户
postgres=# create user scutech password 'dingjia' createdb createrole; CREATE ROLE postgres=# select * from pg_roles where rolname='scutech'; rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig | oid ---------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+------- scutech | f | t | t | t | t | f | -1 | ******** | | f | | 16388 (1 row) postgres=# create database test; CREATE DATABASE postgres=#
从主机上用psql登录
用新创建的scutech用户登录
scutech@infokist:~$ psql -h localhost -U scutech -d test Password for user scutech: psql (9.5.21, server 12.3 (Debian 12.3-1.pgdg100+1)) WARNING: psql major version 9.5, server major version 12. Some psql features might not work. Type "help" for help. test=>
用postgre用户登录
scutech@infokist:~$ psql -h localhost -U postgres Password for user postgres: psql (9.5.21, server 12.3 (Debian 12.3-1.pgdg100+1)) WARNING: psql major version 9.5, server major version 12. Some psql features might not work. Type "help" for help. postgres=#