Thanks to visit codestin.com
Credit goes to github.com

Skip to content

yuanlii/Postgres-in-Docker

Repository files navigation

Connect DB in Docker Postgres

This project is to connect local to Posgres in Docker container. I use Faker and random to generate test data and run a series of SQL queries to test againast the database.

How to Run it

Step 1: Comfigure ODBC

When using Mac OS X, the installer will create a sample user DSN in odbc.ini in either of the following locations:

~/Library/ODBC/odbc.ini
~/.odbc.ini

You may configure your .odbc.ini file by using the following:

[ODBC Data Sources]
my_driver = postodbc

[ODBC]
Trace = 0

[my_driver]
Driver      = /usr/local/lib/psqlodbcw.so
ServerName  = localhost
Port        = 5432
Database    = banking_db
Username    = postgres
Password    = 12345

And configure your odbcinst.ini file by using the following:

[ODBC Drivers]
postodbc = Installed

[postodbc]
Description     = PostgreSQL ODBC driver
Driver          = /usr/local/lib/psqlodbcw.so
Setup           = /usr/local/lib/psqlodbcw.so
Debug           = 0

Step 2: Set up local environment

Cd to your project directly, and create a virtual environment by using the following command:

$ virtualenv venv

and activate the virtual environment that you created:

$. ./venv/bin/activate

then you may install all dependencies using the following:

$ pip install -r requirements.txt

Step 3: Prepare Docker environment

First, pull postgres image from Docker hub by using

$ docker pull postgres:9.6

I use postgres version 9.6 here, but you may choose other versions. Depending on your needs, you may pull other images from Docker hub, for example, other images I pulled include python, busybox, etc.

Step 4: Build and run Docker image

In order to do that, you may simply run the setup.sh:

$ . ./setup.sh

Step 5: Connect to Postgres in Docker and populate test data

In project folder, you may run the test.py file by using:

$ python test.py

This process include three tasks:

  • connect to ​Postgres running in the docker via pyodbc​​
  • populate banking_db with mock test data generated by Faker and Random
  • run demonstration SQL queries

Step 6: Check database

After the database is successfully built, you may use the following command to enter the Docker container:

$ docker exec -it custom_psql_running /bin/sh

Then you may enter the database by using the following command:

psql -h localhost -U postgres -d postgres

Till this step, you should be able to connect to the database that we created by using:

postgres=# \c banking_db;

Also take a look at the tables that we created earlier:

banking_db=# \dt;

You should be able to see all three tables listed, including customer, account, customer_account.

Then you may play around with the database with SQL queries, for example:

select * from customer limit 5;

This will give you the top 5 customer records in the database. Have fun!

*References:

About

Connect DB in Docker Postgres using pyodbc and Docker.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published