How to install Local Postgres on Mac, Ubuntu, CentOS, or Windows
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 postgresqlAfter 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 postgresqlWith 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 mydatabaseRun this command to connect to the database you just created.
psql mydatabaseInstall on Ubuntu (apt-get)
The following command will install the latest version of Postgres available in the Ubuntu package repository.
sudo apt-get install postgresqlAfter 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 startBy 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 --interactiveYou can also set a password for the user account to connect to the Postgres console, run
sudo -u postgres psqland 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 myuserNow 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 myuserOnce 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 updateInstall Postgres:
sudo yum install postgresql-server postgresql-contribInitialize the database:
sudo postgresql-setup initdbStart the Postgres Service:
sudo systemctl start postgresqlAnd enable Postgres to start on Boot:
sudo systemctl enable postgresqlCreate a new user:
sudo -u postgres createuser --interactiveOptionally, 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
- 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: