How do I use Postgres in Bitbucket Pipelines

How do I get a postgres database to run with my pipelines?

1 answer

1 accepted

1 vote

You'll need to use services in bitbucket pipelines

In the most simple cases you can connect to localhost:5432 with database 'postgres', user 'postgres' and no password.

pipelines:
  default:
    - step:
        image: node
        script:
          - npm install
          - npm test
        services:
          - postgres
 
definitions:
  services:
    postgres:
      image: postgres

If you want to specify a different user or password; the below yaml will create a database server on localhost:5432 with database 'pipelines', user 'test_user' and password 'test_user_password'

pipelines:
  default:
    - step:
        image: node
        script:
          - npm install
          - npm test
        services:
          - postgres
 
definitions:
  services:
    postgres:
      image: postgres
      environment:
        POSTGRES_DB: pipelines
        POSTGRES_USER: test_user
        POSTGRES_PASSWORD: test_user_password

nothing about the psql command ? I would like to use this to prepare the database before I run test ....

Hi I tried following your answer here but seems I'm missing something 

image: node:8.1.3

definitions:
services: postgres: image: postgres
environment: POSTGRES_DB: postgres POSTGRES_USER: postgres DATABASE_URL: postgres://postgres@127.0.0.1:5432/testdb pipelines: default: - step: caches:
- node - postgres script: # Modify the commands below to build your repository. - psql -c 'drop database if exists testdb;' -U postgres - psql -c 'create database testdb;' -U postgres
- npm install - npm run model - npm test services:
- postgres

I figured out the error in my config file. Thanks

I have followed these steps as well, running a node image, and I have the postgres service up and running. 

I also have a set of .sql files in my project that defines various tables etc. that I need for integration testing.

In my bitbucket-pipelines.yml file, under the script section, I also have the "psql -U postgres -f \"xyz.sql\"" command to initialise the database.

When Pipelines is running this, I get an error message that "psql: not found"... 

If the above example is running, can anyone point me in the direction of how to get this running in my project?

I have tried to do the same setup in my pipeline.
I run a Node image (as parent), has set up the Postgres service, and can see that they are both running in the Pipeline when picked up by BitBucket.

 

I have an issue running the scripts though. I get the error message "psql: not found".

 

How to be able to run a given set of scripts, located in my *.sql files in the project, to enable integration tests?

I figure I can create my own Docker image, install Postgres in this image, and then run it from there, but doesn't that defeat the entire purpose of using the services?

Any help is appreciated :)

You will probably need to install the psql client manually.

 

So add the below line of installation script before you invoke psql

sudo apt-get update && sudo apt-get install -y postgresql-client

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

85 views 0 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you