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.
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
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
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.
In order to do that, you may simply run the setup.sh:
$ . ./setup.sh
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
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:
- Docker curriculum - https://docker-curriculum.com/
- Docker Docs, Get Started, Part 1: Orientation and setup - https://docs.docker.com/get-started/
- Faker Documentation - https://faker.readthedocs.io/en/master/