迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

使用 Docker Postgres 创建数据库用户

作者:迹忆客 最近更新:2023/04/17 浏览次数:

在开发应用程序时,我们通常会使用 PostgreSQL、MySQL、MongoDB 等数据库管理系统来记录应用程序的数据。

Docker 帮助我们运行这些应用程序数据库管理系统的一个实例。 这有助于节省时间和计算机存储空间,因为计算机上不需要 DBMS。

从这些 DBMS 创建的每个数据库都有对数据库具有不同权限的用户。 在本文中,我们将学习使用 Docker Postgres 创建数据库用户的不同方法。


创建一个新项目

对于本教程,我们将使用 WebStorm IDE,但您可以随意使用任何开发环境。 打开 WebStorm IDE 并选择“文件”>“新建”>“项目”以创建一个新项目。

选择 Empty project 选项,然后在打开的窗口中,将项目名称从 untitled 更改为 initdb.d 或使用任何首选名称。


为用户和数据库定义 DDL

要使用 PostgreSQL 基础映像向我们的自定义映像添加额外的初始化,我们需要在文件夹 /docker-entrypoint-initdb.d 下添加 *.sql*.sql.gz*.sh

由于我们要创建一个 SQL 查询,因此我们将使用扩展名为 .sql 的文件来定义我们的查询。 在我们项目的当前文件夹下创建一个名为 db-config.sql 的文件,并将以下 SQL 指令复制并粘贴到该文件中。

CREATE USER doe;
CREATE DATABASE employee_database;
GRANT ALL PRIVILEGES ON DATABASE employee_database TO doe;

db-config.sql 将在入口点调用 initdb 以创建默认 Postgres 用户和数据库后运行。

请注意 ,/docker-entrypoint-initdb.d 中的脚本仅在数据目录为空时运行。 这意味着在启动期间,在此之前运行的任何数据库都不会更改。


为镜像定义一个 Dockerfile

在当前文件夹下创建一个名为 Dockerfile 的文件,并将以下说明复制并粘贴到该文件中。

FROM postgres:15.1-alpine
COPY db-config.sql /docker-entrypoint-initdb.d/
  1. FROM - 定义使用后续指令在其上创建自定义图像的基础图像。 在这种情况下,我们使用了 alpine,它可以帮助我们优化存储,因为它是 PostgreSQL 的轻量级版本。
  2. COPY - 将文件和文件夹从主机复制到图像文件系统。 在这种情况下,我们已将 db-config.sql 文件复制到 /docker-entrypoint-initdb.d/ 文件夹中。

构建镜像

使用键盘上的键盘快捷键 ALTF12 打开一个新的终端窗口,并使用以下命令构建带有标签 postgres-image 的图像。

~/WebstormProjects/initdb.d$ docker build --tag postgres-image:latest .

输出:

 => [1/2] FROM docker.io/library/postgres:15.1-alpine@sha256:cc663286e63810373bdfc91a5ed24b772447fb5282d  0.0s
 => CACHED [2/2] COPY db-config.sql /docker-entrypoint-initdb.d/                                          0.0s
 => exporting to image                                                                                    0.4s
 => => exporting layers                                                                                   0.0s
 => => writing image sha256:fd33d80c880452dcb25de1d8f7d6415eeb874039bdab176cc3d3fe1c910ebcbc              0.1s
 => => naming to docker.io/library/postgres-image:latest

运行容器

使用相同的终端窗口,使用以下命令运行名为 postgres-container 的 PostgreSQL 容器。

~/WebstormProjects/initdb.d$ docker run --name postgres-container -e POSTGRES_PASSWORD=postgres -d postgres-image

输出:

3b8e0f85c2b4ef4b1aa28e2bad169ae796751331580af6fbba251a1c05aa4fca

请注意 ,我们在运行命令中使用了名为 POSTGRES_PASSWORD 的环境变量。 运行 PostgreSQL 时,我们可以传递几个环境变量来提供超级用户的详细信息,包括数据库和用户名。

POSTGRES_PASSWORD 环境变量是提供超级用户密码的必需变量。 PostgreSQL 的默认密码是 postgres。

可以传递的其他环境变量是可选的,其中包括 POSTGRES_USER、POSTGRES_DB 和 POSTGRES_INITDB_ARGS 等。

由于我们在分离模式下运行容器,因此我们看不到 db-config.sql 文件的执行。 但是,该文件由超级用户在幕后执行。


连接到容器

我们需要使用交互式 shell 连接到容器,以便我们能够使用新用户和数据库登录到 PostgreSQL。 为此,请使用以下命令连接到容器。

~/WebstormProjects/initdb.d$ docker exec -it postgres-container bash

输出:

bash-5.1#

登录到 PostgreSQL

一旦我们可以访问容器的 shell,使用以下命令以用户 doe 身份登录到 employee_database。

bash-5.1# psql -d employee_database -U doe

输出:

psql (15.1)
Type "help" for help.

employee_database=>

在没有脚本文件的情况下创建用户数据库

在上一节中,我们学习了如何通过将包含 DDL 的 SQL 添加到 /docker-entrypoint-initdb.d/ 来在 Docker Postgres 中创建数据库用户。

有一种更简单的方法可以在不编写任何脚本文件的情况下执行此操作。 此方法使用上一节中提到的环境变量来定义新用户的详细信息。

这些环境变量使用 ENV 指令添加到 Dockerfile 中,并将可供容器使用。 要查看实际效果,请将 Dockerfile 中的说明替换为下面提供的说明。

FROM postgres:15.1-alpine
ENV POSTGRES_USER=doe
ENV POSTGRES_DB=employee_database

添加环境变量后,重复前面章节中的所有步骤,从构建镜像、运行容器、连接容器到登录PostgreSQL。

对所有步骤使用相同的命令。 但是,请确保停止并删除现有容器。

您还可以选择创建新的镜像和容器。


总结

在本文中,我们学习了两种可用于通过 Docker Postgres 创建数据库用户的方法。 第一种方法通过向 /docker-entrypoint-initdb.d/ 添加 SQL 脚本来创建用户,并且该文件在 initdb 初始化期间执行。

在第二种方法中,我们通过利用 Docker Postgres 环境变量在 Dockerfile 中定义了用户详细信息。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

发布时间:2025/03/26 浏览次数:107 分类:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

发布时间:2025/03/26 浏览次数:97 分类:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

发布时间:2025/03/26 浏览次数:124 分类:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

发布时间:2025/03/26 浏览次数:202 分类:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便