Run a docker PostgreSQL instance locally for Testing
Running a PostgreSQL instance ad-hoc for testing is not always as bootstraping as it should be. This blog will run a PostgreSQL instance that connects to your workstation’s network and instead of using one of the popular tools like dbeaver we shall use the client that comes with the instance. Also we shall run a bootstrap script to have some data pre-inserted.
Let’s get started by running the instance. On purpose I will use another port. On scenarios of multiple instances running in your workstation, port collisions are likely. The workaround would be to choose port 5433.
This will run PostgreSQL and you shall be able to connect to port 5433. On a CTRL-C the instance will be stopped and destroyed.
Now instead of using an external tool to connect let’s use the instance itself, it comes with psql pre-installed.
The instance works and connections from the outside are possible.
Next step would be to bootstrap a db initialization script.
Supposing the file with the script is called init_db.sh
Let’s run the command with the initialization schema mounted.
And let’s check the results.
That’s it! You created a Postgresql database through docker, you did connect to it also you added a bootstrap script with data.
Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Run a docker PostgreSQL instance locally for Testing
Opinions expressed by Java Code Geeks contributors are their own.
You can also copy *.SQL files to the /docker-entrypoint-initdb.d/ directory.
I’ve done it with the COPY command in a dockerfile:
COPY *.sql /docker-entrypoint-initdb.d/