open-source relational database management system
- numeric types
numeric: stores numbers
- boolean types
boolean
- character types
CHAR(10): fixed-length character stringVARCHAR(200): variable-length character stringTEXT: variable unlimited-length string
- date/time types
DATE: date (no time)TIME: time (no date)TIMESTAMP: date and timeINTERVAL: time interval
- binary types
BYTEA: binary data
- other types
UUID: universally unique identifierJSON: JSON dataJSONB: binary JSON dataARRAY: array of other typesENUM: enumerated list of values
to use postgresql interactive terminal:
sudo -u postgres psql-u postgres: run as the postgres userpsql: open the PostgreSQL prompt
to create user and database:
postgres=# CREATE USER lenny WITH PASSWORD 'leonard';
postgres=# CREATE USER lenny WITH PASSWORD 'leonard';
postgres=# CREATE DATABASE my_project OWNER lenny;
postgres: the database I'm currently connected to
postgresql interactive terminal commands:
- list databases:
\l - list all commands:
\? - connect to a database:
\c dbname - describe a table:
\d tablename - describe/list tables:
\dt - describe/list users:
\du - exit postgres prompt:
\q - create new database:
CREATE DATABASE your_database_name;
Important
use snake_case in postgresql, avoid camelCase
psql -h localhost -U username -d database_name 5432- doesn't expose the password in the command history
string that contains all the information needed to connect to a database
- format depends on the database
- postgresql format:
protocol://username:password@localhost:5432/dbnameprotocol: can bepostgresqlorpostgresusername:password: the credentialslocalhost: the host5432: the port that postgresql is running ondbname: the database name
- can be used in
.envas credentials for the application- format:
DATABASE_URL=postgresql://<DB_USER>:<DB_PASSWORD>@<RDS_ENDPOINT>:5432/<DB_NAME> - e.g.
DATABASE_URL=postgresql://admin:mysecurepassword@your-db-instance.cle8abcd1234.sa-east-1.rds.amazonaws.com:5432/mydatabase
- format:
| Component | Value |
|---|---|
| Protocol | postgresql:// |
| User | <DB_USER> |
| Password | <DB_PASSWORD> |
| Host (RDS Endpoint) | <RDS_ENDPOINT> |
| Port | 5432 |
| Database Name | <DB_NAME> |
SERIAL: auto-incrementing integer
CREATE TABLE users (
id UUID PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
);-
UUID: Universally Unique IDentifier- ensures each value is globally unique
-
PRIMARY KEY: makes it the primary key for the table -
RETURNING: returns the values of the columns listed after the keyword
INSERT INTO employees (name, age, is_manager, salary)
VALUES ('John Doe', 30, true, 50000)
RETURNING *;error message after running psql <connection string>:
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: Ident authentication failed for user "postgres"
-
the problem occurs because by default, postgresql is configured to use "ident" authentication for local connections. this means:
- postgresql checks if your linux system username matches the postgresql username you're trying to use
- if you try to connect as a "postgres" user, but you are not logged in as a "postgres" linux user, you will get this error
-
the connection string uses password authentication, but the default pg_hba.conf ignores the password and tries to use ident instead
-
the solution is to execute a command as
postgresuser to change the authentication method in the pg_hba.conf file
sudo -u postgres vi /var/lib/pgsql/data/pg_hba.confchange this line from pg_hba.conf, replace ident with md5:
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident