Postgres (SQL) - Create a new table using the psql command

by
Jeremy Canfield |
Updated: July 19 2024
| Postgres (SQL) articles
The psql command with a "create table" statement can be used to create a new table.
If using the default posgres user account, I typically go with the following command.
sudo -u postgres psql --command "create table mytable (id serial primary key not null, date_created timestamp, firstname varchar(30) not null, lastname varchar(50) not null)"
If you have created a user account, the -u or --username option and --password flag can be used.
psql --username johndoe --password --command "create table mytable (id serial primary key not null, date_created timestamp, firstname varchar(30) not null, lastname varchar(50) not null)"
Better yet, you can setup passwordless authentication using the hidden .pgpass file and then connect without the --password flag.
psql --username johndoe --dbname mydb --command "create table mytable (id serial primary key not null, date_created timestamp, firstname varchar(30) not null, lastname varchar(50) not null)"
Did you find this article helpful?
If so, consider buying me a coffee over at