Back-end Engineering Articles
I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...
2019-12-06
➜ ~ sudo su - postgres [sudo] password for daniel: postgres@daniel-MS-7A15:~$ psql psql (13.3 (Ubuntu 13.3-1.pgdg16.04+1), server 9.6.22) Type "help" for help. postgres=# \c newsletters psql (13.3 (Ubuntu 13.3-1.pgdg16.04+1), server 9.6.22) You are now connected to database "newsletters" as user "postgres". newsletters=# \l newsletters=# \d newsletters Did not find any relation named "newsletters". newsletters=# \d customers Table "public.customers" Column | Type | Collation | Nullable | Default ---------------+---------+-----------+----------+--------- customer_id | integer | | not null | customer_name | text | | not null | address | text | | not null | Indexes: "customers_pkey" PRIMARY KEY, btree (customer_id) newsletters=#
newsletters=# ALTER TABLE customers newsletters-# ADD phone_number integer; ALTER TABLE newsletters=# \d customers Table "public.customers" Column | Type | Collation | Nullable | Default ---------------+---------+-----------+----------+--------- customer_id | integer | | not null | customer_name | text | | not null | address | text | | not null | phone_number | integer | | | Indexes: "customers_pkey" PRIMARY KEY, btree (customer_id) newsletters=#
newsletters=# SELECT * FROM customers; customer_id | customer_name | address | phone_number -------------+-------------------+-------------------+-------------- 1 | Allie Rahaim | 123 Broadway | 2 | Jacquline Diddle | 456 Park Ave. | 3 | Lizabeth Letsche | 789 Main St. | 4 | Jessia Butman | 1 Columbus Ave. | 5 | Inocencia Goyco | 12 Amsterdam Ave. | 6 | Bethann Schraub | 29 Monticello | 7 | Janay Priolo | 81 Harrisburg | 8 | Ophelia Sturdnant | 31 Deerfield Ave. | 9 | Eryn Vilar | 56 Morton St. | 10 | Jina Farraj | 100 Bryan Ave. | (10 rows)
newsletters=# ALTER TABLE customers newsletters=# ALTER COLUMN phone_number TYPE VARCHAR;
newsletters=# \d customers Table "public.customers" Column | Type | Collation | Nullable | Default ---------------+-------------------+-----------+----------+--------- customer_id | integer | | not null | customer_name | text | | not null | address | text | | not null | phone_number | character varying | | | Indexes: "customers_pkey" PRIMARY KEY, btree (customer_id)
newsletters=# ALTER TABLE customers newsletters-# DROP COLUMN phone_number; ALTER TABLE newsletters=# SELECT * FROM customers; customer_id | customer_name | address -------------+-------------------+------------------- 1 | Allie Rahaim | 123 Broadway 2 | Jacquline Diddle | 456 Park Ave. 3 | Lizabeth Letsche | 789 Main St. 4 | Jessia Butman | 1 Columbus Ave. 5 | Inocencia Goyco | 12 Amsterdam Ave. 6 | Bethann Schraub | 29 Monticello 7 | Janay Priolo | 81 Harrisburg 8 | Ophelia Sturdnant | 31 Deerfield Ave. 9 | Eryn Vilar | 56 Morton St. 10 | Jina Farraj | 100 Bryan Ave. (10 rows)