首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CircleCi有时无法在构建中连接到Postgres

CircleCi有时无法在构建中连接到Postgres
EN

Stack Overflow用户
提问于 2018-08-16 22:06:15
回答 1查看 528关注 0票数 1

基本上,我有一个通过knex连接到postgres数据库的Nodejs项目,我使用CircleCI进行测试。它可以工作,但有时构建会因为“Error: connect ECONNREFUSED 127.0.0.1:5432”而失败,我不知道为什么;它在我的机器上工作没有问题,构建一定有区别。

用来创建测试数据库的文件如下:

代码语言:javascript
复制
'use strict';

require('dotenv').config();

const connectionOptions = require(`${process.cwd()}/knexfile.js`)[`${process.env.NODE_ENV}`];

const knex = require('knex')(connectionOptions);
console.log('New connection to default postgres database made');

// Remove all other connections to test database
knex.raw(`select pg_terminate_backend(pid) from pg_stat_activity where datname = '${process.env.PG_TEST_DATABASE}'`)
	.then(() => {
		console.log('Removed all other connections to the test database');
		// Drop test database if it exists
		return knex.raw(`DROP DATABASE IF EXISTS ${process.env.PG_TEST_DATABASE};`);
	})
	.then(() => {
		console.log('Dropped test database (if it existed)');
		// Create test database
		return knex.raw(`CREATE DATABASE ${process.env.PG_TEST_DATABASE};`);
	})
	.then(() => {
		console.log('Test database created');
		return process.exit();
	});

当它发生时,错误将发生在我连接后第一次尝试使用postgres做任何事情时,无论我是否尝试删除db,删除其他连接,等等。目前,它发生在我尝试执行‘select pg_terminate_backend’行时。

我对Postgres的设置是:

代码语言:javascript
复制
const connectionOptions = {
	client: 'pg',
	version: '7.4.1',
	connection: {
		host: '127.0.0.1',
		user: process.env.PG_USER,
		password: process.env.PG_PASSWORD,
		database: process.env.PG_DATABASE,
		port: parseInt(process.env.PG_PORT) || 5432
	},
	pool: {
		min: 2,
		max: 10
	},
	migrations: {
		tableName: '_migrations',
		directory: './migrations',
	},
	seeds: {
		directory: './seeds/development'
	}
};

我的circleci yml文件如下:

代码语言:javascript
复制
version: 2
jobs:
  build:
    working_directory: ~/project
    docker:
      - image: circleci/node:8.9.4
      # The below environemnt is where you set the .env variables used throughout node
        environment:
          NODE_ENV: test
          PG_USER: jerrodq2
          PG_DATABASE: freelancing_project
          PG_TEST_DATABASE: freelancing_project_test
          PG_PORT: 5432

      - image: postgres:10.3
        environment:
          POSTGRES_USER: jerrodq2
          POSTGRES_DB: freelancing_project

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run:
          name: Install local dependencies
          command: npm install

      - run:
          name: Create database
          command: npm run db:reset:test



      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # run tests!
      - run:
          name: Running Tests
          command: npm run test

我有cross-posted this question to CircleCI Discourse

EN

回答 1

Stack Overflow用户

发布于 2018-08-23 03:21:29

对于任何感兴趣的人,我都能想出一个解决方案。根据上面评论中的Linas建议,我找到了circleci的适当代码,它在进入下一步之前等待postgres启动,我只是添加了一个命令,在创建数据库之前等待5432端口打开,如下所示:

代码语言:javascript
复制
- run:
    name: Install local dependencies
    command: npm install

- run:
    name: Wait for Postgres to start
    command: dockerize -wait tcp://localhost:5432 -timeout 1m

- run:
    name: Create database
    command: npm run db:reset:test

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51879011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档