Tutorial for installing PostgreSQL on Mac OS

  • 2020-05-06 11:52:12
  • OfStack

Let me get off on the wrong foot. I have been working well with MySQL, an old and tested friend. But after learning a bit about PostgreSQL, I became fascinated by its rich features. For example, field types natively support json, xml, and array. Compared to MySQL, PostgreSQL feels more advanced.

installs brew

Official documentation:
To install Git, open an shell:

cd /usr/local
sudo mkdir homebrew
curl -L https://github.com/mxcl/homebrew/tarball/master | sudo tar xz --strip 1 -C homebrew
cd homebrew/bin
./brew -v
file brew
cat brew | moresudo ./brew update

If the "brew update" command executes incorrectly, make sure that the owner of the folder /usr/local is you and not root:

sudo chown $USER /usr/localbrew updat

Update path configuration
in ".bash_profile" If there is no file ".bash_profile "under ~, touch '.bash_profile ')

vim '.bash_profile'


export PATH=$PATH:/usr/local/homebrew/bin

You can then directly execute brew(no./brew)
If you have Git you can install (untested)
like this

git clone https://github.com/mxcl/homebrew.git
cd homebrew/bin
cd homebrew/bin
./brew -v

Install test

./brew install wget
./brew uninstall wget
./brew searc /apache*/

install PostgreSQL

Use brew.

brew install postgres

initializes the database

initdb /usr/local/var/postgres

starts or stops the database

Startup service:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Out of service:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

If it's too much trouble at first, you can add boot up

sudo chown $USER /usr/localbrew updat

common operations

1. Create an PostgreSQL user

sudo chown $USER /usr/localbrew updat

username above is the user name. Enter the user password twice before the user is created. More user creation information can be viewed in "createuser --help".

2. Create database

sudo chown $USER /usr/localbrew updat

A database named dbname is created above, and username is specified as the owner of the changed database (owner). The code of the database (encoding) is UTF8.

More database creation information can be viewed in "createdb --help".

3. Connect to database

psql -U username -d dbname -h


Still in the process of learning, I think it is not much different from MySQL, which is the standard SQL statement. So far, the limit statement is different from MySQL. For example, the first 10 records are taken from MySQL :

sudo chown $USER /usr/localbrew updat

In PostgreSQL it is :

sudo chown $USER /usr/localbrew updat

At first glance, the PostgreSQL looks clearer. offset in MySQL is optional, and it's easy to confuse the offset and limit values at the beginning.

Related articles: