我正在使用CircleCI作为持续集成管道。当CircleCI运行构建时,它将启动一个包含PostgreSQL数据库的Docker容器,并执行一个Python脚本来尝试连接到该数据库。 Python脚本失败,并显示以下错误:
pyodbc.InterfaceError :(“ IM002”,“ [IM002] [unixODBC] [Driver Manager]数据源名称未找到,并且未指定默认驱动程序(0)(SQLDriverConnect)”)
这是Python脚本:
import pyodbc
conn = 'driver={PostgreSQL Unicode};server=0.0.0.0;port=5432;database=data_quality;pwd=password;uid=postgres;'
connection = pyodbc.connect(conn)
print('Success')
CircleCI VM中的文件 /etc/odbcinst.ini 包含以下配置:
[PostgreSQL ANSI]
Description = ODBC driver for PostgreSQL (ANSI version)
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbca.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
Debug = 0
CommLog = 1
[PostgreSQL Unicode]
Description = ODBC driver for PostgreSQL (Unicode version)
Driver = /usr/lib/x86_64-linux-gnu/odbc/psqlodbcw.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcpsqlS.so
Debug = 0
CommLog = 1
我很确定问题是由于CircleCI配置引起的,但我不知道为什么。这是CirclCI配置文件 .config.yml
# .circleci/config.yml
version: 2
jobs:
build:
machine: true
steps:
- checkout
- run:
name: Create Docker Compose configuration file
command: |
echo "POSTGRES_USER=postgres" >> ./.env
echo "POSTGRES_PASSWORD=password" >> ./.env
echo "DATABASE_URL=postgres://postgres:password@db:5432/data_quality" >> ./.env
- run:
name: Build Docker images
command: |
docker volume create data-quality-db-volume
docker-compose -f docker-compose.yml -f docker-compose.test.yml up db
docker ps -a
- run:
name: Install Unix ODBC
command: |
sudo apt-get update
sudo apt-get install -y g++ unixodbc-dev
- run:
name: Install PostgreSQL ODBC driver
command: |
sudo apt-get install -y odbc-postgresql
- run:
name: Move ODBC configuration file
command: |
sudo cp ./scripts/init/odbcinst.ini /etc
cat /etc/odbcinst.ini
- run:
name: Install Python environment
command: |
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
sudo apt-get install python3-pip
- run:
name: Install Python dependencies
command: |
sudo pip3 install --upgrade pip
sudo pip3 install pyodbc
sudo pip3 install nose2 --ignore-installed six
sudo pip3 list
- run:
name: Test Pyodbc connection to postgreSQL
command: |
echo 'export PYTHONPATH=./test:$PYTHONPATH' >> $BASH_ENV
source $BASH_ENV
nose2 --plugin nose2.plugins.junitxml --junit-xml -v
- store_artifacts:
path: test/junit/test-results.xml
destination: test-results
- store_test_results:
path: test