How-To

How to Set Up Local Postgres on Your Mac, Ubuntu, CentOS, or Windows

Mila
Mila4 min read
How to Set Up Local Postgres on Your Mac, Ubuntu, CentOS, or Windows

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:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once you have Homebrew installed, you can install Postgres by running the command

brew install postgresql

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.

brew services start postgresql

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.

createdb mydatabase

Run this command to connect to the database you just created.

psql mydatabase

Install on Ubuntu (apt-get)

The following command will install the latest version of Postgres available in the Ubuntu package repository.

sudo apt-get install postgresql

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.

sudo service postgresql start

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.

sudo -u postgres createuser --interactive

You can also set a password for the user account to connect to the Postgres console, run

sudo -u postgres psql

and then run (replacing "username" and "password" with the username and password you want to use)

ALTER USER username WITH PASSWORD 'password';

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)

createdb mydatabase -O myuser

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).

psql mydatabase -U myuser

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:

sudo yum update

Install Postgres:

sudo yum install postgresql-server postgresql-contrib

Initialize the database:

sudo postgresql-setup initdb

Start the Postgres Service:

sudo systemctl start postgresql

And enable Postgres to start on Boot:

sudo systemctl enable postgresql

Create a new user:

sudo -u postgres createuser --interactive

Optionally, you can set a password for the new user:

sudo -u postgres psql
postgres=# \password <user_name>

Create a new database:

sudo -u postgres createdb <database_name>

Install on Windows

  1. Download the Postgres installer for Windows from the official website.
  2. Run the downloaded installer and follow the installation wizard.
  3. 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.
  4. After the installation is complete, open the "pgAdmin" tool, which should have been installed along with Postgres.
  5. 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.
  6. From the context menu, select "Connect Server".
  7. In the "Connect to Server" dialog, enter the following information:
    1. Host: localhost
    2. Port: 5432
    3. Username: postgres
    4. Password: (the password you set during installation)
  8. Click "OK" to connect to the Postgres server.
  9. 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".
  10. 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:

  1. Top Free Tools to Start a Local Database Instance on Mac
  2. Top SQL AI Tools
  3. Top Postgres Extensions
  4. Top Postgres GUI Clients
  5. Postgres vs. MySQL
  6. Postgres vs. MongoDB

Jointhe community

At Bytebase, we believe in the power of collaboration and open communication, and we have a number of communities that you can join to connect with other like-minded.

Subscribe to Newsletter

By subscribing, you agree with Bytebase's Terms of Service and Privacy Policy.