Server Side Swift with Vapor: Configuring a Database | Ray Wenderlich

@alexhedley Awesome, Iā€™m glad you got it working! :thumbsup:

1 Like

What did you reinstall, exactly? I have the same issues that you had, and reinstalling postgres didnā€™t fix it.

I ran

brew uninstall <same package name>

finally have a chance to sit down and go through all the screencasts with time and some detail. I wanted to do something a bit differently because I already have mySQL installed.

as just a note I suspect my paths are different since I installed mySQL from a DMG versus brew. so I had to edit the compiler and linker settings to have the correct path settings.

compiler addition
-I/usr/local/mysql/include

linker addition
-L/usr/local/mysql/lib

hope this helps that might run into problems that I had.

1 Like

alos trying to clear the version but still not updating the commands

i found the fix for Alex problem ( ie. stuck after postgres -D /usr/local/var/postgres`)
Dont try to quit or anything, just open a new terminal window and execute the commands in the new window (ie create db)

1 Like

One thing to note.

If you configure your Droplet the way it is one it in the screen cast it will break your CSS. Use the code below to fix this problem:

try drop.addProvider(VaporPostgreSQL.Provider.self)

Thereā€™s been a few updates since this screencast was created. Here are a few extra steps that I had to take to get the project running:

  1. No secrets folder under Config.
    Solution: Make your own secrets folder by using command + n while youā€™re in the Config folder.
  2. As @calebkleveter pointed out, you need to use try drop.addProvider(VaporPostgreSQL.Provider.self). Compiler now warns if you try to use the initializer that takes in providers.
1 Like

Thanks @kelvin_lau! :]

@dniswhite hi,
I think that I have the same problem (which is installing mysql from DMG and not from homebrew). I get errors with the package when I compile. my question is how did you edit the compiler and linker? I mean what exact steps should I take.

I had to change the project settings like I listed in my post.

unfortunately I found that really wasnā€™t the best option best every time you generate with vapor xcode you lose those settings and have to do it all over again. the best option is to remove mySQL and install via homebrew.

I just scrapped mySQL and left things the way they were and moved on with postgreSQL.

hi ray im having an issue when saving cant figure out the issue
thats the error im getting
{
ā€œcodeā€: 500,
ā€œerrorā€: true,
ā€œmessageā€: ā€œThe operation couldnā€™t be completed. (PostgreSQL.DatabaseError error 1.)ā€,
ā€œmetadataā€: null
}

@rotemd1 Have you tried setting the user name and database name in the json file to your macOS user name, which is displayed in the Terminal ?

by the way, @rwenderlich. recently the entire screencast videos are not played in Safari 10.0.2. so I am using Chrome for the moment. is this happening only to me?

I just had a similar issue and resolved it.

First, what I initially did wrong is that I had added the wrong link into the Package.swift file. I put the driver in (GitHub - vapor-community/postgresql-driver: PostgreSQL driver for Fluent), and ran ā€œvapor Xcodeā€. Then I corrected it but got ā€œwarning: refname ā€˜1.0.3ā€™ is ambiguous.ā€ errors.

Fetching Dependencies [Failed]
Error: warning: refname ā€˜1.0.0ā€™ is ambiguous.
warning: refname ā€˜1.0.0ā€™ is ambiguous.
warning: refname ā€˜1.0.5ā€™ is ambiguous.
warning: refname ā€˜1.0.5ā€™ is ambiguous.
swift-package: error: rename error: Directory not empty (66): /Users/ronanocsono/Documents/Code/PersonalProjects/vapor/HelloPersistance/Packages/postgresql-driver.git ā†’ /Users/ronanocsono/Documents/Code/PersonalProjects/vapor/HelloPersistance/Packages/FluentPostgreSQL-1.0.5

To fix it, cd into the Packages and ā€œrm -rfā€ all those related to PostgreSQL:

rm -rf FluentPostgreSQL-1.0.5
rm -rf postgresql-provider
rm -rf postgresql-driver.git
rm -rf PostgreSQL-1.1.0
rm -rf CPostgreSQL-1.0.0

Then cd ā€¦/ and run ā€œvapor xcodeā€ again and it works.

Hi I installed postgresql using Homebrew. Command ā€˜postgres -D /usr/local/var/postgresā€™ results in an error I couldnā€™t solve for the past 3 days:
FATAL: postmaster became multithreaded during startup
HINT: Set the LC_ALL environment variable to a valid locale.
LOG: database system is shut down
After researching, I set the local variables in .bash_profile by adding these lines:
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
This was the only suggestion I could find on the Internet but it didnā€™t help, the error stayed the same (I tried restarting the computer after setting the variables, uninstalling and installing postgresql). I ran out of options, every kind of advice is really appreciated. The version is 9.6.3.

Hi Ray,

Nice Tutorial. An update to get things working:

Package.swift
.Package(url: ā€œGitHub - vapor/vapor: šŸ’§ A server-side Swift HTTP web framework.ā€, majorVersion: 2),
.Package(url: ā€œGitHub - vapor-community/postgresql-provider: PostgreSQL Provider for the Vapor web framework.ā€, majorVersion: 2),
.Package(url: ā€œGitHub - vapor-community/fluent-provider: A provider for including Fluent in Vapor applicationsā€, majorVersion: 1)

Config+Setup.swift
import PostgreSQLProvider

private func setupProviders() throws {
try addProvider(FluentProvider.Provider.self)
try addProvider(PostgreSQLProvider.Provider.self)
}

Routes.swift:
import PostgreSQLProvider

get(ā€œversionā€) {req in
let version = try self.postgresql().raw(ā€œSELECT version()ā€);
return try JSON(node: version)
}

Config/postgresql.json. (not secret)
{
ā€œhostnameā€: ā€œ127.0.0.1ā€,
ā€œuserā€: ā€œwhoamiā€, (current user)
ā€œpasswordā€: ā€œā€,
ā€œdatabaseā€: ā€œwhoamiā€, (current database)
ā€œportā€: 5432
}

fluent.json
ā€œdriverā€: ā€œpostgresqlā€,

And it works. Keep up the good work Ray!

1 Like

Thanks for sharing the updates! :]

Hi there guysā€¦ @rwenderlich this is my error when call localHost:8080/version:

[SQLite.StatusError: error(ā€œno such function: versionā€)]
Conform ā€˜SQLite.StatusErrorā€™ to Debugging.Debuggable to provide more debug information.

And in the brower is error 500

Thanks