SoFunction
Updated on 2025-03-03

Detailed steps to install PostgreSQL database in Docker

1. Start the PostgreSQL container

docker run --name ffj-postgres -p 5432:5432 -e POSTGRES_PASSWORD=Cisc0123 -d postgres
  • docker run: Start a new container.
  • --nameSpecify the container name as ffj-postgres
  • -p 5432:5432: Map the host's port 5432 to the container's port 5432.
  • -e POSTGRES_PASSWORD=Cisc0123: Set up PostgreSQLpostgresThe user's password isCisc0123
  • -d postgres: Run the container in the background and usepostgresMirror.

2. Get the IP address of the container

docker inspect -f '{{.}}' ffj-postgres
  • docker inspect: View the container details.
  • -f '{{.}}': Format the output to display only the IP address.
  • ffj-postgres: Specify the name of the container to view.

The output of this step will be the container's IP address, for example172.17.0.2

3. Start a new CentOS container

docker run --rm -it --name ffj-centos1 centos
  • docker run: Start a new container.
  • --rm: Automatically delete the container when the container exits.
  • -it: Make the container run in interactive mode and allocate a pseudo-terminal.
  • --name ffj-centos1: Specify the container name as ffj-centos1
  • centos:usecentosMirror.

4. Install the PostgreSQL client in the CentOS container

yum install -y postgresql
  • yum install -y: Automatically confirm the installationpostgresqlBag

Since centos stopped maintaining, the source needs to be replaced. The container does not have a vim command, and echo is used

echo "[BaseOS]
name=CentOS-\$releasever - Base
baseurl=/centos-vault/8.5.2111/BaseOS/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[AppStream]
name=CentOS-\$releasever - AppStream
baseurl=/centos-vault/8.5.2111/AppStream/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

[extras]
name=CentOS-\$releasever - Extras
baseurl=/centos-vault/8.5.2111/extras/\$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
" > /etc//

yum clean all && yum makecache

5. Connect to PostgreSQL container through the psql client

psql -U postgres -d postgres -h 172.17.0.3
  • psql: PostgreSQL command line client.
  • -U postgres:usepostgresUser connection.
  • -d postgres: Connect topostgresdatabase.
  • -h 172.17.0.3: Specify the IP address of the PostgreSQL server.

6. Perform SQL operations in PostgreSQL

The following is the SQL operation after connecting to PostgreSQL:

  • Switch topostgresdatabase
\c postgres
  • Create a tabletest1
create table test1(t1 int, t2 varchar(40));
  • Insert data
insert into test1(t1, t2) values (11, 'welcome to fengfujiang');
insert into test1(t1, t2) values (12, 'welcome to python');
  • Query data
select * from test1;

Summarize

This is the end of this article about Docker installation PostgreSQL database. For more related Docker installation PostgreSQL content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!