Skip to content

Commit

Permalink
Update files
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpotts committed Nov 5, 2021
1 parent 445596c commit 552585b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ USER postgres
# Expose ports
EXPOSE 1433 5432

# Set start command
# Set entry point
ADD start.sh /
CMD [ "/start.sh" ]
ENTRYPOINT [ "/start.sh" ]
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,56 @@ To create a new container, run:

`docker run -d -p 1433:1433 jonathanpotts/babelfishpg`

### Example Data

Use the [example_data.sql](https://github.com/jonathanpotts/docker-babelfishpg/blob/main/example_data.sql) script to populate the database with example data.

You can then query the database using commands such as:

```sql
SELECT * FROM example_db.authors;
```

```sql
SELECT * FROM example_db.books;
```

### Advanced Setup

To initialize with a custom username, append `-u my_username` to the `docker run` command where `my_username` is the username desired.

To initialize with a custom password, append `-p my_password` to the `docker run` command where `my_password` is the password desired.

To initialize with a custom database name, append `-d my_database` to the `docker run` command where `my_database` is the database name desired. **This is the name of the database that Babelfish for PostgreSQL uses internally to store the data and is not accessible via TDS.**

#### Migration Mode

By default, the `single-db` migration mode is used.
To use a different migration mode, append `-m migration_mode` to the `docker run` command where `migration_mode` is the value for the migration mode desired.

For more information about migration modes, see [Single vs. multiple instances](https://babelfishpg.org/docs/installation/single-multiple/).

## Connecting

If you are hosting the container on your local machine, the server name is `localhost`. Otherwise, use the IP address or DNS-backed fully qualified domain name (FQDN) for the server you are hosting the container on.

Use SQL Server Authentication mode for login.

The default login for Babelfish is:

* **Username:** `babelfish_user`
* **Password:** `12345678`

If you specified a custom username and/or password, use those instead.

Babelfish does not currently support the SSMS Object Explorer. If you are using SSMS, you must connect via a **New Query**.

### Connection string

Assuming Babelfish is hosted on the local machine, using the default settings, and you are trying to connect to a database named `example_db`, the connection string is:

`Data Source=localhost;Initial Catalog=example_db;Persist Security Info=true;User ID=babelfish_user;Password=12345678`

## Data Volume

Database data is stored in the `/data` volume.
35 changes: 35 additions & 0 deletions example_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- Create example_db schema
CREATE SCHEMA example_db;
GO

DROP TABLE IF EXISTS example_db.books;
DROP TABLE IF EXISTS example_db.authors;

-- Create tables
CREATE TABLE example_db.authors (
author_id INT IDENTITY PRIMARY KEY,
[name] NVARCHAR (MAX) NOT NULL,
);

CREATE TABLE example_db.Books (
book_id INT IDENTITY PRIMARY KEY,
title NVARCHAR (MAX) NOT NULL,
author_id INT NOT NULL,
publish_date DATETIME2 NOT NULL,
price MONEY NOT NULL,
FOREIGN KEY (author_id) REFERENCES example_db.authors (author_id) ON DELETE CASCADE ON UPDATE CASCADE
);
GO

-- Seed tables
INSERT INTO example_db.authors ([name]) VALUES
('Kristin Hannah'),
('Andy Weir');

INSERT INTO example_db.books (title, author_id, publish_date, price) VALUES
('The Four Winds', 1, '02-02-2021 00:00:00', 14.99),
('The Nightingale', 1, '02-03-2015 00:00:00', 11.99),
('Project Hail Mary', 2, '05-04-2021 00:00:00', 14.99),
('Artemis', 2, '11-14-2017 00:00:00', 8.99),
('The Martian', 2, '02-11-2014 00:00:00', 8.99);
GO
33 changes: 25 additions & 8 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,39 @@

cd /usr/local/pgsql/bin

# Set default argument values
USERNAME=babelfish_user
PASSWORD=12345678
DATABASE=babelfish_db
MIGRATION_MODE=single-db

# Populate argument values from command
while getopts u:p:d:m: flag
do
case "${flag}" in
u) USERNAME=${OPTARG};;
p) PASSWORD=${OPTARG};;
d) DATABASE=${OPTARG};;
m) MIGRATION_MODE=${OPTARG};;
esac
done

# Initialize database cluster if it does not exist
[ ! -f /data/postgres/postgresql.conf ] && ./initdb -D /data/postgres \
&& printf "# Allow all connections\nhost\tall\t\tall\t\t0.0.0.0/0\t\tmd5\nhost\tall\t\tall\t\t::0/0\t\t\tmd5\n" >> /data/postgres/pg_hba.conf \
&& printf "\n# Configure babelfish\nshared_preload_libraries = 'babelfishpg_tds'\n" >> /data/postgres/postgresql.conf \
&& ./pg_ctl -D /data/postgres start \
&& ./psql -c "CREATE USER babelfish_user WITH CREATEDB CREATEROLE PASSWORD '12345678' INHERIT;" \
-c "DROP DATABASE IF EXISTS babelfish_db;" \
-c "CREATE DATABASE babelfish_db OWNER babelfish_user;" \
-c "\c babelfish_db" \
&& ./psql -c "CREATE USER ${USERNAME} WITH CREATEDB CREATEROLE PASSWORD '${PASSWORD}' INHERIT;" \
-c "DROP DATABASE IF EXISTS ${DATABASE};" \
-c "CREATE DATABASE ${DATABASE} OWNER ${USERNAME};" \
-c "\c ${DATABASE}" \
-c "CREATE EXTENSION IF NOT EXISTS \"babelfishpg_tds\" CASCADE;" \
-c "GRANT ALL ON SCHEMA sys to babelfish_user;" \
-c "ALTER SYSTEM SET babelfishpg_tsql.database_name = 'babelfish_db';" \
-c "GRANT ALL ON SCHEMA sys to ${USERNAME};" \
-c "ALTER SYSTEM SET babelfishpg_tsql.database_name = '${DATABASE}';" \
-c "ALTER SYSTEM SET babelfishpg_tds.set_db_session_property = true;" \
-c "ALTER DATABASE babelfish_db SET babelfishpg_tsql.migration_mode = 'single-db';" \
-c "ALTER DATABASE ${DATABASE} SET babelfishpg_tsql.migration_mode = '${MIGRATION_MODE}';" \
-c "SELECT pg_reload_conf();" \
-c "CALL SYS.INITIALIZE_BABELFISH('babelfish_user');" \
-c "CALL SYS.INITIALIZE_BABELFISH('${USERNAME}');" \
&& ./pg_ctl -D /data/postgres stop

# Start postgres engine
Expand Down

0 comments on commit 552585b

Please sign in to comment.