-
-
Save mightyhorst/73b75995afc5eb9e8ea893e2269e6981 to your computer and use it in GitHub Desktop.
example docker compose for postgresql with db init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postgres: | |
image: postgres:9.4 | |
volumes: | |
# ~~~~~~~~~~~~~~ | |
# | |
# @name Migrations and Seeders | |
# @description mapping each file as an example only, in reality you would just map the entire seeders folder | |
# @warning the order is important | |
# | |
# @param path_on_your_computer:path_to_init_db_folder_in_postgres_container | |
# | |
# @usage ./seeders:/docker-entrypoint-initdb.d/ | |
# | |
- ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql | |
- ./data.sql:/docker-entrypoint-initdb.d/2-data.sql | |
- ./relationships-and-constraints.sql:/docker-entrypoint-initdb.d/3-relationships-and-constraints.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP DATABASE [IF EXISTS] database_name; | |
CREATE DATABASE database_name; | |
# @name migrations | |
# @desctipion table names | |
create table sometable(id int); | |
# @name create table | |
# @description create a table. repeat this for all tables | |
CREATE TABLE table_name ( | |
column_name TYPE column_constraint, | |
table_constraint table_constraint | |
) INHERITS existing_table_name; | |
CREATE TABLE account( | |
user_id serial PRIMARY KEY, | |
username VARCHAR (50) UNIQUE NOT NULL, | |
password VARCHAR (50) NOT NULL, | |
email VARCHAR (355) UNIQUE NOT NULL, | |
created_on TIMESTAMP NOT NULL, | |
last_login TIMESTAMP | |
); | |
CREATE TABLE link ( | |
ID serial PRIMARY KEY, | |
url VARCHAR (255) NOT NULL, | |
name VARCHAR (255) NOT NULL, | |
description VARCHAR (255), | |
rel VARCHAR (50) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add data for seeders | |
INSERT INTO link (url, name) | |
VALUES | |
('http://www.postgresqltutorial.com','PostgreSQL Tutorial'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# relationships and constraints | |
ALTER TABLE child_table | |
ADD CONSTRAINT constraint_name FOREIGN KEY (c1) REFERENCES parent_table (p1); | |
ALTER TABLE child_table | |
ADD CONSTRAINT constraint_fk | |
FOREIGN KEY (c1) | |
REFERENCES parent_table(p1) | |
ON DELETE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# If you want to copy the schema from an existing database you can use this executable | |
# | |
databasename= | |
hostname= | |
port=5432 | |
username= | |
password= | |
schemaname= | |
locationofthedumpfile=_postgres_dump.sql | |
pg_dump -d $databasename -h $hostname -U $username -f $locationofthedumpfile | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment