How to Install PostgreSQL on a MAC
Installation
Environment: macOS Sierra 10.12.4.
Install this database with ternimal:
Install with
brew
1
brew install postgresql
After
brew
installation, initialization of DB1
initdb /usr/local/var/postgres
Start server
1
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Database and user creation
There is no users after installation, use command:
Create your first database
1
createdb
Login PostgreSQL console
1
psql
Using
\l
could list all the information by name, owner, encoding, collate, Ctype and access privileges.\q
for quit.After database creation, users are also needed to be created.
Create postgres user
1
CREATE USER postgres WITH PASSWORD 'password';
Delete default database
1
DROP DATABASE postgres;
Create a database under user postgres
1
CREATE DATABASE postgres OWNER postgres;
Grant all privileges for user postgres
1
GRANT ALL PRIVILEGES ON DATABASE postgres to postgres;
Change attribute of postgres
1
ALTER ROLE postgres CREATEDB;
Now you can use postgres to login, and magege databases under this user.
Login console as a user
1 | psql -U [user] -d [database] -h [host] -p [port] |
Login by psql
, in fact, it’s under default setting.
1 | user: current mac user |
Login user postgres
1 | psql -U postgres -d postgres |
Common console commands
command | meaning |
---|---|
\h | help, e.g. \h select |
\? | list commands |
\l | list databases |
\c [database_name] | connect a database |
\d | list all the tables of current database |
\d [database_name] | list all the tables of a database |
\u | list all the users |
\e | open editor |
\conninfo | print information of current DB and connection |
\password [user] | change user psw |
\q | quit |