I am working on a new learning project to setup a simple Spring java rest api application. As part of this project, the first step was to setup a database backend for my service. For this I am going to use a PostgreSQL database on my laptop. As I do this, I am going to capture all the steps for anyone who may want to do similar things.
My laptop's OS is Xubuntu. So you can use these steps to install the database in Ubuntu or such Linux distributions. Installing in Windows is very similar and differs only in places such as the downloadable to use, the paths used during the installation etc.
- in linux, you can install most softwares using the OS's package manager. For example, in Ubuntu, you could do: sudo apt-get install postgresql postgresql-contrib . Unfortunately my experience has been that these package managers have old versions of software. So I prefer installing from the distribution website sometimes.
- First download the postresql installable (appropriate one for your OS) from EnterpriseDB's website. Mine being a linux 64 bit system, i downloaded the below one.
- This .run file cannot be executed immediately. You will need to first set its executable permission to allow for the execution (this step is specific to unix systems)
- Now you can double click the executable to get the installation popup
- Click Next to proceed. There will be a series of inputs that you will provide like folder to install postgreSQL, folder for data, password for the superuser(postgres), port number etc.
- At this point, the installation will start. I choose to stick with the default values provided by postgres
- Post the installation, update your environment variable PATH to include the postgreSQL installation's BIN folder. Below is an update I am adding in my .profile file in the linux OS for this -
- Thats it, you are done with the installation. Now lets bring up the pgAdmin III tool to connect to the database server and create a database. A default database will be created by the installation. I will be creating another one for my application. Bring up the pgAdmin tool and connect to the database server -
- You will need the superuser password to connect to the database server. Now you can create a new database. I decided to create a new login role as well so I dont use my superuser login everywhere.
- For creating a new database, you can right click and choose New Database. This will bring up the wizard to guide you through the process. Thats it, its pretty simple.
The final result will be -
- From here, let me show a couple of other useful things you will need (if you love using the terminal like I do). First to start the postgreSQL database server from the terminal, just use below command - sudo service postgres start
- psql is the tool provided by postgres to allow you to connect to the database from the terminal. You can use psql from the terminal by this command - psql DATABASE_NAME USER_NAME
- Once connected, you can query the database as needed -
Thats it folks for this post. A simple first step towards the actual implementation of my api project. Thanks!
Comments
Post a Comment