I was making a web app to deploy using Heroku.com when I realized that the only database type they support is PostgreSQL. Up until now, my app (powered by the Ruby gem Sinatra) accessed the database via the .Sqlite method for the Sequel gem.
Here's my Ruby script from when I used Sequel to access the .db file via SQLite:
DB = Sequel.sqlite('mydatabase.db')
DB.create_table :mytable do
primary_key :id
String :column_name
end
I installed PostgreSQL after learning Heroku used only that. Here's the script via postgres (my username is literally 'postgress', though I obviously won't reveal my password in this question):
DB = Sequel.postgres('mydatabase.db',:user=>'postgres',:password=>'my_password_here',:host=>'localhost',:port=>5432,:max_connections=>10)
DB.create_table :mytable do
primary_key :id
String :column_name
end
However, when I run this code, I get the following error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/sequel-3.38.0/lib/sequel/adapters/postgres.rb:208:in 'initialize': PG::Error: FATAL: database "mydatabase.db" does not exist (Sequel::DatabaseConnectionError)
I've tried searching Google, StackOverflow, Sequel documents, and the Heroku help documents for any help, but I've found no fix to this problem.
Does anyone know what I am doing wrong?
The database mydatabase.db doesn't exist, as per the error message from Pg. Likely reasons:
You probably meant mydatabase without the SQLite-specific .db filename suffix
It's possible you created the db with different case, eg "Mydatabase.db";
You might be connecting to a different Pg server than you think you are
You never created the database. Unlke SQLite's default behaviour, Pg doesn't create databases when you try to connect to a database that doesn't exist yet.
If in doubt, connect to Pg with psql and run \l to list databases, or connect via PgAdmin-III.
The PostgreSQL documentation and tutorial are highly recommended, too. They're well written and will teach you a lot about SQL in general as well as Pg in particular.
BTW, the postgres user is a superuser. You should not be using it for your application; it's like running your server as root, ie a really bad idea. Create a new PostgreSQL user without superuser, createdb or createuser rights and use that for your application. You can either CREATE DATABASE somedb WITH OWNER myappuser - or preferably, create the database owned by a different user to your webapp user and then expicitly GRANT the webapp user the minimum required permissions. See user management and GRANT.
On heroku all you need to do is tell Sequel to connect to the content of the DATABASE_URL environment variable (which is a properly formed url that Sequel understands):
DB = Sequel.connect(ENV['DATABASE_URL'])
Related
From the section to copy another use is to use
sequel -C mysql://host1/database postgres://host2/database2
Is there a way to include username and password for both databases.
Tried with no luck
sequel -C mysql://user:password#host1/database postgres://user2:password2#host2/database2
The usernames and passwords are stored in the user database. You first need to setup a user on your target database. Before that you can't copy anything to it. Copying the user database from mysql to postgres is not going to work either imho so you will have to setup your user manually first.
I'm stumbling on the real basics because I cannot find any clear directions.
I have installed Postgres.app 9.3.5 on OS X.
The documentation tells me how to create a database, createdb mydb, but to use it I'd need a PostgreSQL user and a password for that user.
What are the basic steps to create a PostgreSQL user with a password (and a database if different than the above) on a local PostgreSQL install?
The Postgres.app documentation used to explain this, but appears to have been split up and cut short recently.
You don't need a dedicated user; you can simply use the default postgres user. However, because that's a superuser, you're wise to create less privileged users for your apps.
You generally want to make a database owned by the app's user, as Rails expects to be able to use the same user to change the schema with migrations as it does to run queries. (It's not great security-wise, but it's the path of least resistance with Rails).
So create the user, then create the db.
Here's how it should look. Don't type the prompts ($ or postgres=#), they're there to show what you're running. Just enter the commands after the prompts.
$ psql -q
postgres=# CREATE USER myapp WITH ENCRYPTED PASSWORD 'mypassword';
postgres=# CREATE DATABASE myappdb OWNER myapp;
postgres=# \q
You may now specify the user, password and database in your database.yml.
(Note that Postgres.app may be configured not to require passwords; in this case the password will be ignored by PostgreSQL).
createdb mydb
psql -s mydb
create user space_pilot password 'hello';
GRANT ALL PRIVILEGES ON DATABASE mydb TO space_pilot;
i have just started working on an application in PHP. I have configured the Postgres add on in my application on Heroku. But i am still not sure how to start working on the DATABASE. what i mean is that there is a thing called DATACLIP on the POSTGRES DB window on Heroku and when i try to create a table there, it gives me some weird errors.
But no matter what query i write there, it always gives me some error. So, i am not sure whether i can create the Tables and insert my data from DATACLIP or not. and If not, from where can i do this?
Dataclips are only useful for read-only queries. Think gist for SQL and data.
To create data on your database, you can:
Use database migrations that run within the heroku platform itself
Connect to your database using the provided credentials from anywhere (you must use SSL). To get credentials, either run heroku pg:credentials <DATABASE_COLOR> --app <YOUR_APP>, or go to your database in http://postgres.heroku.com. You should be able to use pg_admin or any other postgres front end to connect and create tables.
You can connect directly using the heroku toolbelt (and assuming you have a proper psql installation in your dev box) with heroku pg:psql --app <YOUR_APP>. From there just use SQL to create your data (CREATE TABLE, etc)
See "UPDATE" below for what I now know is the crux of the problem.
I have a legacy Derby database that I want to make a Rails application to interface with.
I am using RVM so here are the steps I took:
1. rvm install jruby (installed 1.6.7.2)
2. rvm use jruby
3. gem install rails
4. rails new myapp
5. add "gem 'activerecord-jdbcderby-adapter'" to Gemfile of new rails app
6. bundle
7. copied my derby db folder (named 'perm') under db in the rails app
8. changed database.yml as follows:
development:
adapter: jdbcderby
database: db/perm
Then I made a model file and used set_table_name to set it to one of the table names and when I run rails c I get an exception that the table does not exist.
Also in the console when I do
ActiveRecord::Base.connection.tables
the only table that comes back is "schema_migrations".
I know there is nothing wrong with the database as I can connect to this exact copy and see all the tables using SquirrelSQL. Also, the rails app is connecting in some way since when I open the console I cannot, while the console is running, connect to the same instance using SquirrelSQL, and vice versa.
Does anyone have any suggestion as to why active record doesn't see the tables?
UPDATE:
The problem has something to do with how derby "organizes" tables into multiple "schemas" in the same file. When I create a new table from rails (i.e. with a migration) the table ends up in the "SA" schema. All of my legacy tables are in the "APP" schema (maybe I could move them but I don't want to do that if I can avoid it...other apps would break). So when I access the db from rails this way it's like only the "SA" schema exists. How do I tell Rails to 'use' the "App" schema (early on I tried prefacing the table name but that didn't work)?
I retitled the question accordingly.
UPDATE #2:
Apparently the jdbcderby gem supports the "schema" setting. On a guess I tried changing my database.yml to the following:
development:
adapter: jdbcderby
database: db/perm
schema: app (also tried APP)
When I have the app (or APP) schema setting when I do ActiveRecord::Base.connection.tables in the console I get the list of tables from the app schema (the list shows up with table names all lower case with no schema name).
But I am still having trouble accessing the tables. When I make a model file on an existing table and try to access it I get a JDBCError: "Schema 'SA' does not exist". I have tried various set_table_name calls with no success.
As far as I know there is nothing unusual about my database. But there is no information anywhere on how to do this. Am I the only person on earth who has ever tried to use a legacy Derby database with Rails?
The database name 'db/perm' is being interpreted relative to your current working directory, which is probably not the directory that you think it is. Try either (1) searching your hard disk for the file 'derby.log', which is probably being created in the actual current working directory of your rails app, or (2) specifying the absolute file path to your derby database, not a relative file path.
If the problem is the schema, Derby supports the SET SCHEMA statement: http://db.apache.org/derby/docs/10.8/ref/rrefsqlj32268.html
If you don't explicitly set the schema, it defaults to the username that you connect with, so you can also indirectly set the schema by logging in as the desired username.
I have an installer wizard thing called 'install creator'. I want to include my mySQL database into the installer or find another way that the user, upon installation, can just use my database. Prob is-not everyone has MySQL installed on the computer and even then, the user doesn't know the name of the database or my password. Somehow the database must be created automatically upon install, and for my purposes, some of the tables created. How can one do this. Thanks
If you are just using MySQL as a local storage engine, as it seems to be what you are doing, then you should consider using Sqlite with JDBC, instead of MySQL. MySQL is really intended to be used on a server, where information from multiple users is stored, and where the database is accessed only indirectly through the programs that you create that run on the server. You could, in theory, package up MySQL and MySQL Connector/J which lets JDBC talk with MySQL; however, MySQL is a pretty big beast, and I don't think it's nice to do that to your users (also, don't forget that they might already have MySQL installed, and if you were to install MySQL for the first time, you would effectively be forcing them to use your root password). Unlike MySQL, sqlite is intended to provide the structure of SQL for use with lightweight, local file storage.