9951 explained code solutions for 126 technologies


postgresqlHow do I install and use PostgreSQL on a Mac?


  1. Download and install PostgreSQL from https://www.postgresql.org/download/macosx/.
  2. Open the terminal, and run the following command to start the PostgreSQL server:
$ pg_ctl -D /usr/local/var/postgres start
  1. Create a database:
$ createdb mydb
  1. Connect to the database:
$ psql mydb
  1. Execute SQL queries:
mydb=# CREATE TABLE users (
  id serial PRIMARY KEY,
  username VARCHAR (50) UNIQUE NOT NULL,
  password VARCHAR (50) NOT NULL
);
  1. To check the table, run the following command:
mydb=# \d users
  1. Output:
              Table "public.users"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           | not null |
 username | character varying(50) | | not null |
 password | character varying(50) | | not null |
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

For more information, please refer to the PostgreSQL Documentation.

Edit this code on GitHub