{"id":160,"date":"2022-02-10T12:49:35","date_gmt":"2022-02-10T12:49:35","guid":{"rendered":"https:\/\/tejasgarde.dev\/?p=160"},"modified":"2022-02-10T12:49:36","modified_gmt":"2022-02-10T12:49:36","slug":"postgres-database-and-pg-admin-using-docker-compose","status":"publish","type":"post","link":"https:\/\/tejasgarde.dev\/index.php\/2022\/02\/10\/postgres-database-and-pg-admin-using-docker-compose\/","title":{"rendered":"Postgres Database and PG Admin using Docker Compose"},"content":{"rendered":"\n<p>Docker makes all development things really easy. Just one line command and you are up with whatever you want (of course if it&#8217;s available as docker Image \ud83d\ude09).  <\/p>\n\n\n\n<p>Gone are those days when you have to spend hour or so to setup just a simple database on you local development environment , worrying about process , ports etc.  Docker makes it really easy for you . In this article we will use a simple docker compose file to setup the PostgreSQL data base as well as PG Admin. <\/p>\n\n\n\n<p>Here is the docker compose file to set it up. Save this code as docker-compose.yaml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: \"3\"\nservices:\n  db:\n    container_name: my-pg-db\n    image: postgres:13.4-buster\n    restart: always\n    environment:\n      POSTGRES_USER: pguser\n      POSTGRES_PASSWORD: pgpassword\n      POSTGRES_DB: mydatabase\n    ports:\n      - \"5432:5432\"\n    volumes:  # Persist the db data\n      - database-data:\/var\/lib\/postgresql\/data\n  pgadmin:\n    container_name: my-pgadmin\n    image: dpage\/pgadmin4\n    restart: always\n    environment:\n      PGADMIN_DEFAULT_EMAIL: mypg@example.com\n      PGADMIN_DEFAULT_PASSWORD: Hello123\n    ports:\n      - \"9080:80\"\nvolumes:\n  database-data:<\/code><\/pre>\n\n\n\n<p>To run this docker container go to the folder in which it is saved and type the following command <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker-compose up<\/code><\/pre>\n\n\n\n<p>The above command will run 2 docker containers , one is postgres db container my-pg-db and other one is PG Admin container my-pgadmin.  You can check these with docker ps command . Now as the PG Admin container is running at port 9080 got to http:\/\/localhost:9080 , you should see page like this.  <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1600\" height=\"976\" src=\"https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.05.14-PM-1600x976.png\" alt=\"\" class=\"wp-image-162\" srcset=\"https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.05.14-PM-1600x976.png 1600w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.05.14-PM-768x468.png 768w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.05.14-PM-1536x937.png 1536w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.05.14-PM-2048x1249.png 2048w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Here you have to login with credentials given in my-pgadmin container .  One login you will have to add server from left side<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1600\" height=\"810\" src=\"https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.07.55-PM-1600x810.png\" alt=\"\" class=\"wp-image-163\" srcset=\"https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.07.55-PM-1600x810.png 1600w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.07.55-PM-768x389.png 768w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.07.55-PM-1536x778.png 1536w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.07.55-PM-2048x1037.png 2048w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>In create Server page , in general section provide any name <\/p>\n\n\n\n<p>In Connection Tab as shown you will have to provide IP address of the my-pg-db container. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1600\" height=\"810\" src=\"https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.10.01-PM-1600x810.png\" alt=\"\" class=\"wp-image-164\" srcset=\"https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.10.01-PM-1600x810.png 1600w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.10.01-PM-768x389.png 768w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.10.01-PM-1536x778.png 1536w, https:\/\/tejasgarde.dev\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-10-at-6.10.01-PM-2048x1037.png 2048w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>to get IP address of the container , type below command <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker inspect &lt;CONTAINER_ID_OF_my-pg-db> | grep IPAddress<\/code><\/pre>\n\n\n\n<p>the IP address returned by this command should be used at Host Name\/address field .<br>Other fileds are similar to environment variables provided to my-pg-db in you docker-compose.yaml file. Click on save and you will see the database listed in you server.<\/p>\n\n\n\n<p>To access this database from  your development environment just use IP as localhost and port as mention in docker file (int this case 53432)<\/p>\n\n\n\n<p>for example your JDBC url should look like for postgres database<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jdbc:postgresql:\/\/localhost:5432\/postgres<\/code><\/pre>\n\n\n\n<p>That&#8217;s it , it is as simple as that . <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker makes all development things really easy. Just one line command and you are up with whatever you want (of course if it&#8217;s available as docker Image \ud83d\ude09). Gone are those days when you have to spend hour or so to setup just a simple database on you local development environment , worrying about process &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/tejasgarde.dev\/index.php\/2022\/02\/10\/postgres-database-and-pg-admin-using-docker-compose\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Postgres Database and PG Admin using Docker Compose&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/posts\/160"}],"collection":[{"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":1,"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/posts\/160\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tejasgarde.dev\/index.php\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}