Skip to content

Instantly share code, notes, and snippets.

@psahni
Last active January 7, 2025 07:29
Show Gist options
  • Save psahni/b54cda8c3703ab7239202ddfd48427ea to your computer and use it in GitHub Desktop.
Save psahni/b54cda8c3703ab7239202ddfd48427ea to your computer and use it in GitHub Desktop.
all about postgres

Connect

psql -U postgres

Create Db

CREATE DATABASE my_test_db

List Db

\l

Use Db

\c my_test_db

List Tables

\d

Show Tables

\c database_name;

Alter Table

ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name INTEGER;

ALTER TABLE table_name DROP COLUMN IF EXISTS column_name;

Connect DB

psql -U postgres -d northwind

@psahni
Copy link
Author

psahni commented Jan 28, 2024

@psahni
Copy link
Author

psahni commented Mar 11, 2024

@psahni
Copy link
Author

psahni commented Oct 2, 2024

Get comma separated data

You can use the array() and array_to_string() functions together with your query.
With SELECT array( SELECT id FROM table ); you will get a result like: {1,2,3,4,5,6}

Then, if you wish to remove the {} signs, you can just use the array_to_string() function and use comma as separator, so: SELECT array_to_string( array( SELECT id FROM table ), ',' ) will get a result like: 1,2,3,4,5,6

@psahni
Copy link
Author

psahni commented Nov 13, 2024

@psahni
Copy link
Author

psahni commented Jan 6, 2025

pg_hba.conf path

psql -U postgres -c 'show hba_file'

@psahni
Copy link
Author

psahni commented Jan 6, 2025

Restart on mac

brew services restart postgresql@16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment