PostgreSQL is quite popular, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.
You can download PostgreSQL from https://www.postgresql.org/
When you install it you will getHere is a good tutorial for PostgreSQL http://www.postgresqltutorial.com/
In Windows you can configure how the PostgreSQL starts with below commands
Note
The following command connects to a database under a specific user. After pressing Enter, PostgreSQL will ask for the password.
psql -d database -U user
To connect to a database on another host, add the -h option:
psql -h host -d database -U user
Once connected, you can use the following backslash commands inside the psql shell:
| Command | Description |
|---|---|
\c dbname [username] | Switch connection to a different database |
\l | List all databases |
\dt | List tables in the current schema |
\dt schema.* | List tables in a specific schema |
\dn | List all schemas |
\d tablename | Describe a table (columns, types, constraints) |
\di | List indexes |
\dv | List views |
\df | List functions |
\du | List roles and users |
\timing | Toggle display of query execution time |
\i filename | Execute SQL commands from a file |
\o filename | Send query output to a file |
\e | Open the last query in a text editor |
\g | Re-execute the previous command |
\? | Show help for psql backslash commands |
\h [command] | Show help for SQL commands (e.g. \h SELECT) |
\q | Quit psql |
If you are familiar with MySQL, PostgreSQL will feel familiar in many ways. However, there are important differences worth knowing before you start.
PostgreSQL has a richer type system than MySQL. The table below highlights the most common differences:
| MySQL | PostgreSQL | Notes |
|---|---|---|
INT AUTO_INCREMENT | SERIAL / INT GENERATED ALWAYS AS IDENTITY | See the next section |
TINYINT(1) | BOOLEAN | PostgreSQL has a true boolean type; use TRUE / FALSE |
VARCHAR(n) | VARCHAR(n) or TEXT | TEXT has no length limit and is fully equivalent in performance |
DATETIME | TIMESTAMP | Use TIMESTAMPTZ when time zone awareness is needed |
JSON | JSON / JSONB | JSONB stores data in binary form and supports indexing; prefer JSONB |
| — | UUID | See the next section |
| — | ARRAY | Columns can store arrays of any type, e.g. INTEGER[] |
MySQL uses AUTO_INCREMENT to create auto-incrementing columns. PostgreSQL has two approaches:
SERIAL (traditional, still widely used):
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name VARCHAR(100)
);
GENERATED AS IDENTITY (SQL standard, introduced in PostgreSQL 10) comes in two variants:
-- Strict: manual inserts are rejected; the value is always generated
CREATE TABLE person (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name VARCHAR(100)
);
-- Flexible: a generated value is used unless you supply one explicitly
CREATE TABLE person (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(100)
);
Use GENERATED ALWAYS when the identity column should never be set manually (safer for surrogate keys).
Use GENERATED BY DEFAULT when you occasionally need to insert an explicit value, for example when migrating data from another database.
A UUID (Universally Unique Identifier) is a 128-bit value represented as a 32-character hex string, for example a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11. PostgreSQL has a built-in UUID type that stores it efficiently in 16 bytes.
UUID is an alternative to integer primary keys. The main reasons to use it:
The downside is that UUID primary keys are larger (16 bytes vs 4 bytes for INT) and because they are random, inserting many rows can fragment indexes and slow down range scans compared to sequential integers.
In PostgreSQL 13+ you can generate a UUID with the built-in function gen_random_uuid() — no extension needed:
CREATE TABLE order_item (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
order_id UUID NOT NULL,
product VARCHAR(100),
quantity INT
);
-- UUID is generated automatically on insert
INSERT INTO order_item (order_id, product, quantity)
VALUES ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'Laptop', 1);
-- You can also supply your own UUID explicitly
INSERT INTO order_item (id, order_id, product, quantity)
VALUES (gen_random_uuid(), gen_random_uuid(), 'Mouse', 2);
In earlier versions, enable the pgcrypto extension first:
CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- then use gen_random_uuid() in the same way
As a rule of thumb: use IDENTITY (integer) for most tables, and consider UUID when you need globally unique IDs across systems or want to hide row counts from users.
Under the hood, SERIAL and IDENTITY both use a sequence — a database object that generates a series of unique numbers. You can also create and use sequences directly:
CREATE SEQUENCE order_seq START 1000 INCREMENT 10;
SELECT nextval('order_seq'); -- returns the next value
SELECT currval('order_seq'); -- returns the current value
SELECT setval('order_seq', 500); -- set the current value manually
Sequences are independent of tables, so the same sequence can serve multiple tables or be reset without affecting existing data.
In PostgreSQL, DDL statements (CREATE TABLE, DROP TABLE, ALTER TABLE, etc.) can be included inside a transaction and rolled back if something goes wrong.
BEGIN;
CREATE TABLE test_table (id SERIAL PRIMARY KEY, name TEXT);
INSERT INTO test_table (name) VALUES ('Alice');
ROLLBACK; -- both the table and the row are gone
In MySQL, DDL statements cause an implicit commit and cannot be rolled back. PostgreSQL DDL transactions make schema migrations significantly safer.
PostgreSQL supports a RETURNING clause on INSERT, UPDATE, and DELETE statements. It returns the affected rows immediately, removing the need for a separate SELECT.
-- Get the generated id after insert
INSERT INTO person (name) VALUES ('Alice') RETURNING id;
-- Get the updated row after update
UPDATE person SET name = 'Bob' WHERE id = 1 RETURNING id, name;
-- Get the deleted row
DELETE FROM person WHERE id = 1 RETURNING *;
MySQL has no equivalent; you must use LAST_INSERT_ID() or run a separate query.
In MySQL, LIKE is case-insensitive by default for most collations. In PostgreSQL, LIKE is case-sensitive. Use ILIKE for case-insensitive matching:
-- case-sensitive: matches 'alice', not 'Alice'
SELECT * FROM person WHERE name LIKE 'ali%';
-- case-insensitive: matches 'alice', 'Alice', 'ALICE'
SELECT * FROM person WHERE name ILIKE 'ali%';
In PostgreSQL, unquoted identifiers (table names, column names) are automatically folded to lowercase. Quoting an identifier preserves its case but makes it case-sensitive.
CREATE TABLE Person (Name VARCHAR(100)); -- stored as: person(name)
SELECT * FROM person; -- works
SELECT * FROM Person; -- works (folded to lowercase)
SELECT * FROM "Person"; -- ERROR: relation "Person" does not exist
The safe rule: always use lowercase identifiers and avoid quoting them. If you migrate a MySQL schema with mixed-case names, rename them to lowercase in PostgreSQL.
The most common PostgreSQL client for Node.js is node-postgres (pg). Install it with:
npm install pg
Connect using a connection string and a Pool, which manages multiple simultaneous connections efficiently:
const { Pool } = require('pg');
const pool = new Pool({
connectionString: 'postgresql://libuser:libpass@localhost:5432/library',
});
async function getBooks() {
const res = await pool.query('SELECT * FROM book WHERE author = $1', ['Tolkien']);
return res.rows;
}
Note that PostgreSQL uses $1, $2, ... as parameter placeholders, unlike MySQL which uses ?.
When connecting to a remote server, enable SSL by adding an ssl option. In most cloud environments the server certificate is signed by a trusted CA, so you only need:
const pool = new Pool({
connectionString: 'postgresql://libuser:libpass@db.example.com:5432/library',
ssl: { rejectUnauthorized: true },
});
If you are connecting to a server with a self-signed certificate (common in development), set rejectUnauthorized: false — but never use this setting in production.
In PostgreSQL, a schema is a logical namespace that holds database objects such as tables, views, functions, and indexes. It allows multiple users or applications to organize and isolate data within the same database.
In PostgreSQL, schemas are useful when you need to separate objects for different applications, isolate tenants in a shared database, or keep development, testing, and production structures organized. They also help manage user-specific namespaces and group related objects for easier security and maintenance.
In many cases, creating a separate schema is not necessary. For small projects, single-user databases, or applications with only a few tables, the default public schema is often sufficient. If you don’t need to isolate data, manage multiple environments in the same database, or apply schema-level permissions, using the default schema keeps the database simpler and easier to maintain.
Every PostgreSQL database contains a default schema called public. If no schema is explicitly specified when creating or querying objects, PostgreSQL uses this schema by default.
You can create your own schema with the following SQL command:
CREATE SCHEMA libschema;
When creating objects inside a specific schema, you can prefix the schema name:
CREATE TABLE libschema.book (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
author VARCHAR(255),
isbn VARCHAR(255)
);
To make a schema the default for object lookup, you can set the search path in your session:
SET search_path TO libschema;
After setting this, you can reference tables and other objects without specifying the schema name.
In psql, you can list all schemas using:
\dn
The default administrator is postgres. You have to first login with that user and then you can create new users and give privileges to them.
Example: Create a new user libuser and give privileges to all tables in database library to that user.
CREATE ROLE libuser WITH LOGIN PASSWORD 'libpass';And then you can give permissions to that user, like this
\c library
grant ALL ON SCHEMA public to libuser;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO libuser;
CREATE SCHEMA libschema AUTHORIZATION libuser;
pg_dump works mostly the same way as mysqldump. So you can start it from the command line and backup a database like this
pg_dump -U user database > db.sqlThen you can restore the database with this command
psql -U user -d database -f db.sqlNote: In Windows pg_dump and psql are located in ProgramFiles\PostgreSQL\...\bin
When you have installed PostgreSQL, you probably have also installed pgAdmin, which is a quite handy PostgreSQL-client. If you don't have it yet, you can get it from https://www.pgadmin.org
DBeaver is also quite handy graphical SQL-client. And in fact, you can use it also with MySQL and SQL-server. You can download it from https://dbeaver.io/.
Login to your PostgreSQL as administrator (=postgres) and execute below commands
CREATE DATABASE test; \c test; CREATE ROLE user1 WITH LOGIN PASSWORD 'user1'; CREATE ROLE user2 WITH LOGIN PASSWORD 'user2'; CREATE SCHEMA schema1; CREATE SCHEMA schema2; CREATE TABLE schema1.table1( idTable1 INT primary key, name CHAR(50) ); CREATE TABLE schema2.table2( idTable2 INT primary key, name CHAR(50) ); insert into schema1.table1 VALUES (1,'Jim'); insert into schema2.table2 VALUES (1,'Lisa'); GRANT USAGE ON SCHEMA schema1 to user1; GRANT USAGE ON SCHEMA schema2 to user2; GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema1 to user1; GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema2 to user2;Then login as a user1 (psql -U user1 -d test) and execute below commands. Some of them does not work. Try to figure out why.
\dt SELECT * FROM table1; SELECT * FROM schema1.table1; SELECT * FROM schema2.table2; set search_path to schema1; \dt SELECT * FROM table1;Then you can delete the database and user1 and user2.
user1 and user2 should disconnect first as a administrator \c test DROP TABLE schema1.table1; DROP SCHEMA schema1; DROP USER user1; DROP TABLE schema2.table2; DROP SCHEMA schema2; DROP USER user2; \c postgres DROP DATABASE test;