Setting up / Using PostgreSQL on DOCKER

Hi again! This tutorial is on how to run postgres on Docker.

STEP 1:

Install Docker: And how are you going to do that. Click on the following link [WINDOWS]

To check if docker is installed use the following command on your cmd:

docker run hello-world

And you should get something like this:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

Now if you did not get this, there is some problem with the installation. Check for Hyper-V settings.


Following are some useful commands for docker.

-In order to see the process running you can use the following command:

docker ps

-To view Docker Images:<images are files built up from a series of layers. Each layer represents an instruction>

docker images

Docker run takes an image and creates a  running container

-To create a ubuntu with bash container use:

docker run -ti ubuntu:latest bash

ti: terminal interactive

STEP 2:

POSTGRESQL:

Open your command-line and write the following:

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

-p : port <host port>:<container port>

Create DB inside the container

docker exec -it pgUser bash

root@799523be61b4:/# psql -U postgres
psql (11.2 (Debian 11.2-1.pgdg90+1))
Type “help” for help.

postgres=# CREATE DATABASE cakes;
CREATE DATABASE

Your db is creates in the database. You can exit the postgres using \q and bash using exit.

Next to see the container running and your database (we are running locally now if you are running on server provide the IP)

C:\Users>psql -h localhost -p 5433 -U postgres -W
Password for user postgres:{put password here}
psql (9.6.9, server 11.2 (Debian 11.2-1.pgdg90+1))
WARNING: psql major version 9.6, server major version 11.
Some psql features might not work.                                                                                                postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
———–+———-+———-+————+————+———————–
cakes | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)

Once done we need to connect to pgAdmin to view everything is happening properly.


Step3

pgAdmin

What is pgAdmin? Follow the link

Open pgAdmin, Click on create server.

11

Next provide the name of the postgres container you created, the port and the table details.

Click on ok. And in the panel on the left you can see your database. But before that lets add some data to the database. Lets switch back to the cmd.

postgres=# Create Table cake(id serial PRIMARY KEY, name VARCHAR(50) NOT NULL, price NUMERIC);
CREATE TABLE

postgres=# INSERT INTO cake(name, price) VALUES(‘chocolate’,20);
INSERT 0 1
postgres=# INSERT INTO cake(name, price) VALUES(‘apple’,10);
INSERT 0 1
postgres=# INSERT INTO cake(name, price) VALUES(‘butterscotch’,30);
INSERT 0 1
postgres=# INSERT INTO cake(name, price) VALUES(‘blueberry’,40);
INSERT 0 1
postgres=# INSERT INTO cake(name, price) VALUES(‘almond’,35);
INSERT 0 1
postgres=# select * from cake;
id | name | price
—-+————–+——-
1 | chocolate | 20
2 | apple | 10
3 | butterscotch | 30
4 | blueberry | 40
5 | almond | 35
(5 rows)

Switching back to pgAdmin lets see if we can see our data there.

And vola!!! Our data

output

Hope this was helpful.

Stay tuned for more.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s