Skip to content

Instantly share code, notes, and snippets.

@achraf-jeday
Last active April 20, 2020 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achraf-jeday/0e2d34f8a43034b7d4c7f72c4df943ba to your computer and use it in GitHub Desktop.
Save achraf-jeday/0e2d34f8a43034b7d4c7f72c4df943ba to your computer and use it in GitHub Desktop.
Postgres useful commands
Connect to the Postgres server:
psql postgres postgres
Create a database:
CREATE DATABASE drupal_legacy;
Connect to a database:
\c drupal_legacy
List all tables:
\dt
Select query:
SELECT * FROM opm_ref_document_type;
SELECT * FROM opm_ref_commune WHERE code_postal = '94250';
Count rows query:
SELECT count(*) AS exact_count FROM users_field_data;
List all databases:
\l
Delete a database:
DROP DATABASE IF EXISTS "dev-fo";
Drop a table:
DROP TABLE IF EXISTS opm_ref_diplome;
Quit CLI:
\q
Import an SQL file into a database:
psql drupal_legacy postgres < global_opm_fo_pgdump.sql
Get the last value of a sequence:
SELECT last_value FROM pfe_demande_fo_id_seq;
Set the value of a sequence:
SELECT setval('pfe_demande_fo_id_seq', 1, true); # next value will be 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment