How do I use Postgres in Bitbucket Pipelines

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

2 answers

1 accepted

This widget could not be displayed.

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

@Werner Lindgård Hi Werner. I assume that you managed to solve the "psql: not found" issue. I would greatly appreciate if you could share your solution. Many thanks!

@Atanda Olalekan Could you post your version that worked please.

I'm trying all the ways but cannot make a postgres image work. My best result is:

psql: could not connect to server: No such file or directory

@Sergio Pelin

 

pipelines:
default:
- step:
image: node
script: # Modify the commands below to build your repository.
- npm install
-
npm run migrations
- npm run model
- npm test
services:
- postgres

definitions:
services:
postgres:
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
DATABASE_URL: postgres://postgres@127.0.0.1:5432/postgres

Thanks @Atanda Olalekan!

However, I can see that you finally didn't use `psql` command in this version. Did you succeed using this command in some configuration?

yeah...it wasn't necessary since I already specified that I'm using a Postgres image which bitbucket provides expressly with this configuration.

@Sergio Pelin

the only thing missing in the config I shared is the node image which you can add to the top of this configuration as it is in my failing config earlier in this thread. I hope it works for you too

@Atanda Olalekan My issue is that I want to execute command line instructions via `psql` at the beginning of the test, but I can't find the way to do so...

Perhaps @Sebastian Cole could help me :)

@Sergio Pelin have you tried executing the command in the script section 'cause you should be able to since psql command should be made available as a result of postgres image being available in your test environment. Also, you may want to see if Bitbucket pipelines have a before_script command like TravisCI config so you can have your psql commands there.

@Atanda Olalekan I tried a lot in different ways, but it doesn't work. Hence I opened this question with a bit more detail.

@Sergio Pelin

can you try this in your bitbucket-pipelines.yml and post back what errors you are getting here

image: node:8.1.3

pipelines:
default:
- step:
image: node
script: # Modify the commands below to build your repository.
- psql # your psql command here
services:
- postgres
definitions:
services:
postgres:
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
DATABASE_URL: postgres://postgres@127.0.0.1:5432/postgres

@Atanda Olalekan Here's the log:

+ psql
bash: psql: command not found 

 It seems though, that the service is starting on a wrong address: "0.0.0.0" instead of "127.0.0.1":

2018-07-09 23:35:38.940 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432

Could you see this output in your case?

@Sergio Pelin are you trying to prepopulate your database with the psql command 'cause if that's the case, you may want to use seeders instead, Also, you may want to add the command to your package.json script. it would help me understand the issue better if I know what the command is to do

@Sergio Pelin update the address in that case to "0.0.0.0". I didn't have your issue 'cause my tests didn't need anything that requires me to run psql commands

I found a solution (explained here).

Thanks to @Atanda Olalekan and @Pratik Mandrekar for hints and support!

This widget could not be displayed.

@Sergio Pelin Hello again!
This issue has been on the back-burner for a while, so I have not come up with a good solution yet.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 21, 2018 in Bitbucket

Branch Management with Bitbucket

As a project manager, I have discovered that different developers want to bring their previous branching method with them when they join the team. Some developers are used to performing individual wo...

1,217 views 8 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