Introduction
A local Postgres database is installed and running on your own computer or server, allows developers to work offline without depending on a remote server, and also enables them to quickly iterate on changes without risking data loss or corruption.
Below is a tutorial on how to install it on Mac, Ubuntu, CentOS, and Windows.
Install on Mac
GUI
Check out Top 3 Free Tools to Start a Local Database Instance on Mac.
Homebrew
If you want a GUI tool,
The easiest way to install Postgres on a Mac is to use Homebrew. If you don't have Homebrew installed, you can install it by running the following command in your terminal:
Once you have Homebrew installed, you can install Postgres by running the command
After Postgres is installed, you can start the database server by running this command, which will start the server and make it available to your local machine.
With the Postgres server running, you can create a new database by running the following command, make sure to replace "mydatabase" with the name you want to give your database.
Run this command to connect to the database you just created.
Install on Ubuntu (apt-get)
The following command will install the latest version of Postgres available in the Ubuntu package repository.
After Postgres is installed, you can start the database server by running the following command, which will start the server and make it available to your local machine.
By default, Postgres creates a user account with the same name as your system user, but with no password. To create a user account, you can run the command and follow the prompts to create a new user account.
You can also set a password for the user account to connect to the Postgres console, run
and then run (replacing "username" and "password" with the username and password you want to use)
With the Postgres server running and a user account created, you can create a new database with the following (replacing "mydatabase" with the name you want to give your database, and "myuser" with the username you created in the last step)
Now connect to your new database using the psql command (again, replacing "mydatabase" with the name of your database, and "myuser" with the username you created).
Once you're connected to your database, you can create tables and add data to them using SQL commands.
Install on CentOS (yum)
Open a terminal window on your CentOS machine. You can do this by pressing Ctrl+Alt+T on your keyboard.
Update the package list and upgrade any installed packages by running the following commands:
Install Postgres:
Initialize the database:
Start the Postgres Service:
And enable Postgres to start on Boot:
Create a new user:
Optionally, you can set a password for the new user:
Create a new database:
Install on Windows
- Download the Postgres installer for Windows from the official website.
- Run the downloaded installer and follow the installation wizard.
- During the installation, you will be prompted to set a password for the default Postgres user, which is called "postgres". Make sure to remember this password, as you will need it later.
- After the installation is complete, open the "pgAdmin" tool, which should have been installed along with Postgres.
- In pgAdmin, click on the "Servers" node in the left-hand sidebar, and then right-click on the "Postgres" server that should appear in the main panel.
- From the context menu, select "Connect Server".
- In the "Connect to Server" dialog, enter the following information:
- Host: localhost
- Port: 5432
- Username: postgres
- Password: (the password you set during installation)
- Click "OK" to connect to the Postgres server.
- Once you are connected, you can create a new database by right-clicking on the "Databases" node in the left-hand sidebar, and selecting "Create > Database".
- In the "Create - Database" dialog, enter a name for the new database, and click "Save".
That's it! You should now have a working Postgres database on your own machine, and you can start using it with your preferred client or by using the command line.
More Postgres Tools
If you are trying to learn more about Postgres, or simply looking for tools to help you better work with and manage Postgres, be sure to check these out: