【docker-compose】一键安装PostgreSQL数据库

简介: 【docker-compose】一键安装PostgreSQL数据库

docker-compose】一键安装PostgreSQL数据库


1、创建空目录


[root@docker ~]# mkdir PostgreSQL
[root@docker ~]# cd PostgreSQL/


2、创建docker-compose.yml文件


postgres Tags | Docker Hub


直接下来docker-compose.yml


wget https://raw.githubusercontent.com/colovu/docker-postgres/master/docker-compose.yml


或者编写一个docker-compose.yml


[root@docker PostgreSQL]# ls
data  docker-compose.yml
[root@docker PostgreSQL]# vim docker-compose.yml 
[root@docker PostgreSQL]# cat docker-compose.yml 
version: "3.3"
services:
 postgres:
  image: postgres:12-alpine
  container_name: xybdiy_postgres
  restart: always
  environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
  ports:
    - 5432:5432
  volumes:
    - /root/PostgreSQL/data:/var/lib/postgresql/data


3、一键启动项目


docker-compose up -d


[root@docker PostgreSQL]# docker-compose up -d
Pulling postgres (postgres:12-alpine)...
12-alpine: Pulling from library/postgres
59bf1c3509f3: Already exists
c50e01d57241: Pull complete
a0646b0f1ead: Pull complete
45cbd5b44a2d: Pull complete
902601345251: Pull complete
ab80db796fc0: Pull complete
1d825ba7fe16: Pull complete
91db3a1e8d17: Pull complete
Digest: sha256:0b634f9f533f9ce0411148ce9571ece8bed9475aa74fb3620bac60bf7a521d70
Status: Downloaded newer image for postgres:12-alpine
Creating xybdiy_postgres ... done
[root@docker PostgreSQL]#


4、查看容器


[root@docker PostgreSQL]# docker-compose ps
     Name                    Command              State                    Ports                  
--------------------------------------------------------------------------------------------------
xybdiy_postgres   docker-entrypoint.sh postgres   Up      0.0.0.0:5432->5432/tcp,:::5432->5432/tcp
[root@docker PostgreSQL]# docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED             STATUS             PORTS                                       NAMES
a5a952cf662e   postgres:12-alpine   "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes       0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   xybdiy_postgres
74dde54efbe0   wordpress:latest     "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:8000->80/tcp, :::8000->80/tcp       wordpress_wordpress_1
f22bc03ae9ca   mysql:5.7            "docker-entrypoint.s…"   About an hour ago   Up About an hour   3306/tcp, 33060/tcp                         wordpress_db_1


5、连接postgresql数据库


[root@docker PostgreSQL]# docker exec -it a5a952cf662e bash
bash-5.1# psql -U root -W
Password: 
psql (12.9)
Type "help" for help.
root=# 
# 查询当前时间
root=# select now();
              now              
-------------------------------
 2022-04-23 16:42:10.781947+00
(1 row)
#查询亚洲/上海地区时间
root=# select  now() at time zone 'Asia/Shanghai';
          timezone          
----------------------------
 2022-04-24 00:42:20.457047
(1 row)
# 设置postgres数据库的时区
root=# ALTER DATABASE "postgres" SET timezone TO 'Asia/Shanghai';
ALTER DATABASE
# 使用帮助命令
root=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit


使用Navicat数据库管理工具连接postgresql数据库




6、创建数据库


# 创建数据库xybdiy
root=# CREATE DATABASE xybdiy;
CREATE DATABASE
# 查看已存在的数据库
root=# \list
                             List of databases
   Name    | Owner | Encoding |  Collate   |   Ctype    | Access privileges 
-----------+-------+----------+------------+------------+-------------------
 postgres  | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
 root      | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root          +
           |       |          |            |            | root=CTc/root
 template1 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root          +
           |       |          |            |            | root=CTc/root
 xybdiy    | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
(5 rows)
# 进入数据库xybdiy
root=# \connect xybdiy
Password: 
You are now connected to database "xybdiy" as user "root".
xybdiy=# 


7、创建表格


# 创建表格
xybdiy=# CREATE TABLE COMPANY(
xybdiy(#    ID INT PRIMARY KEY     NOT NULL,
xybdiy(#    NAME           TEXT    NOT NULL,
xybdiy(#    AGE            INT     NOT NULL,
xybdiy(#    ADDRESS        CHAR(50),
xybdiy(#    SALARY         REAL
xybdiy(# );
CREATE TABLE
xybdiy=# 
# 查看表格
xybdiy=# \display
                List of relations
 Schema |     Name     | Type  | Owner |  Table  
--------+--------------+-------+-------+---------
 public | company_pkey | index | root  | company
(1 row)
# 查看表格信息
xybdiy=# \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)
xybdiy=# 


8、删除表格和数据库


# 删除表格
\xybdiy=# drop table company,department;
DROP TABLE
xybdiy=# \d
Did not find any relations.
# 删除数据库
root=# drop database xybdiy;
DROP DATABASE
root=# \l
                             List of databases
   Name    | Owner | Encoding |  Collate   |   Ctype    | Access privileges 
-----------+-------+----------+------------+------------+-------------------
 postgres  | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
 root      | root  | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root          +
           |       |          |            |            | root=CTc/root
 template1 | root  | UTF8     | en_US.utf8 | en_US.utf8 | =c/root          +
           |       |          |            |            | root=CTc/root
(4 rows)
root=# 


至此,使用docker-compose一键安装postgresql数据库完成。

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1040 152
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
815 156
|
8月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
927 213
|
SQL Ubuntu 关系型数据库
PostgreSQL介绍和PostgreSQL包安装
PostgreSQL 是一个功能强大、可扩展的开源关系型数据库系统,以其可靠性、数据完整性和高性能著称。它支持复杂查询、事务、多版本并发控制及丰富的数据类型,适用于各种应用场景。本文介绍 PostgreSQL 的核心特性,并详细说明在多种 Linux 发行版上的安装与配置方法,帮助用户快速部署和使用该数据库系统。
857 0
|
5月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
5月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
5月前
|
Ubuntu 安全 关系型数据库
安装与配置MySQL 8 on Ubuntu,包括权限授予、数据库备份及远程连接指南
以上步骤提供了在Ubuntu上从头开始设置、配置、授权、备份及恢复一个基础但完整的MySQL环境所需知识点。
582 7
|
6月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
622 0
|
9月前
|
SQL 关系型数据库 MySQL
MySQL下载安装全攻略!小白也能轻松上手,从此数据库不再难搞!
这是一份详细的MySQL安装与配置教程,适合初学者快速上手。内容涵盖从下载到安装的每一步操作,包括选择版本、设置路径、配置端口及密码等。同时提供基础操作指南,如数据库管理、数据表增删改查、用户权限设置等。还介绍了备份恢复、图形化工具使用和性能优化技巧,帮助用户全面掌握MySQL的使用方法。附带常见问题解决方法,保姆级教学让你无忧入门!
1198 21
MySQL下载安装全攻略!小白也能轻松上手,从此数据库不再难搞!
|
8月前
|
存储 关系型数据库 分布式数据库
【赵渝强老师】基于PostgreSQL的分布式数据库:Citus
Citus 是基于 PostgreSQL 的开源分布式数据库,采用 shared nothing 架构,具备良好的扩展性。它以插件形式集成,部署简单,适用于处理大规模数据和高并发场景。本文介绍了 Citus 的基础概念、安装配置步骤及其在单机环境下的集群搭建方法。
756 2