Accessing Docker Postgres from host application

postgres on docker

Accessing Docker Postgres in the host application

Accessing docker postgres is was easier with right commands. Docker has been a life saver for most of us – no doubt on that. We can do any kind of software interaction from host machine or from other docker container with bliss.

Once you have the docker postgres up and running, access docker postgres and use it for multiple of your projects. Here I will show how to access docker postgres from host machine. That is the same thing as any other app to access it as well.

On this blog will show you from the scratch on how to get PostgreSQL from docker, access it from the host machine and later I will put the commands needed to have Postgres running locally and use them later.

The good thing about docker is, you can throw out images, mount volumes, update versions without breaking sweat and more and more. So any developer out there, if you haven’t got your foot wet with it, it is the right time to head start with docker.

Get Docker postgres locally

The first is to have postgres locally through docker. You will get more information at https://hub.docker.com/_/postgres

docker pull postgres
This will pull the latest postgres from docker.

Or if you want specific version of docker

docker pull postgres:version

Run postgres container


docker run -p 5400:5432 --name dockerpostgres -e POSTGRES_PASSWORD=YOUR_PASSWORD postgres

If you have specific version you would like to run then do

docker run -p 5400:5432 --name dockerpostgres -e POSTGRES_PASSWORD=YOUR_PASSWORD postgres:12

If you have version 12 for example.

The above command creates and runs a container named dockerpostgres with the given password and username postgres. Also the port 5400 on host will be mapped to 5432 on the docker side.

Access docker from host machine

Assuming you have installed psql, postgres can be accessed from host machine as follows


psql -h localhost -U postgres -p 5400

First access the postgres using postgres user. Once you logged in, create another user that you can use from your applications.


postgres=# create user appuser with password 'testpass';
postgres=# create database appuser owner appuser;

Here create a user and also create a database with the same username.

Then create database, any database the application is going to use for example, and assign the owner


postgres=# create database testdatabase owner appuser;

Here you have created a database called testdatabase and the owner of the database is the user appuser created above.

The next task is to assign privilege to the user


postgres=# grant all privileges on database testdatabase to appuser;
postgres=# grant all privileges on database appuser to appuser;

You might want to be careful on granting privileges to users. You can grant selected privileges to users like select update.. than the blanket privileges.

Exit from psql using \q

Now postgres has new user and new database so log using the user created.


postgres -h localhost -U testuser -p 5400

Psql will ask for the password this time and provide what you have created above.


postgres=# \l

This lists all the databases that the testuser has access to.

postgres not null to null

facebook page post from PHP

spring error ClassNotFoundException org.springframework.web.context.ContextLoaderList

The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

Add batch/multiple posts to Facebook Page – Graph API

Using Facebook API to post articles from PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*